基于下一个最接近的排名创建新列

2024-05-23 19:49:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一份以订阅方式出售的产品清单。价格因地区而异(约15个地区)。我正在努力寻找下一个最接近当天价格的产品(不是当天最便宜的)。我的数据是这样的

data = [['29/10/20', 400, 300, 2, 1], 
       ['29/10/20', 250, 400, 1, 2], 
       ['29/10/20', 600, 600, 3, 3],
       ['30/10/20', 800, 500, 3, 2]
       ['30/10/20', 200, 800, 1, 3], 
       ['30/10/20', 550, 300, 2, 1] 

df = pd.DataFrame(data, columns = ['date', 'east price', 'west price', 'east position', 'west position'])

我希望我的输出看起来像

date     east_price nearest_east_price west_price nearest_west_price 
29/10/20 400        250                300        300 
29/10/20 250        250                400        300
29/10/20 600        400                600        400
30/10/20 800        550                500        300
30/10/20 250        250                800        500
30/10/20 550        250                300        300

我不确定是否要根据现有的职位信息进行此操作,或者是否有其他方法进行此操作。我整天都在忙这个。我还有一个问题,那就是当天最便宜的产品,我希望它能返回它的价值。有人能帮忙吗?我是编程新手,所以可能会错过一些显而易见的东西


Tags: 数据dataframedfdatadate产品方式position
1条回答
网友
1楼 · 发布于 2024-05-23 19:49:20

编辑:(2020年7月2日)。OP需要输出,因此如果存在重复的价格,则最近的价格不能相同

请参阅下面添加了np.wherebfill()的新解决方案

import pandas as pd
import numpy as np
data = [['29/10/20', 400, 300, 2, 1], 
       ['29/10/20', 250, 400, 1, 2], 
       ['29/10/20', 600, 600, 3, 3],
       ['30/10/20', 800, 500, 3, 2],
       ['30/10/20', 200, 800, 1, 3], 
       ['30/10/20', 550, 300, 2, 1]]

df = pd.DataFrame(data, columns = ['date', 'east_price', 'west_price', 'east_position', 'west_position'])

for col in df.columns:
    if '_price' in col:
        df = df.sort_values(col)
        nearest_col = f'nearest_{col}'
        df[nearest_col] = (np.where((df.shift(-1)[col] == df[col]), np.nan, df.shift(-1)[col]))
        df[nearest_col] = df[nearest_col].bfill()
df

输出:

    date        east price  west price  east position   west position   nearest_east_price  nearest_west_price
0   29/10/20    400        300          2               1               550.0   400.0
5   30/10/20    550        300          2               1               600.0   400.0
1   29/10/20    250        400          1               2               400.0   500.0
3   30/10/20    800        500          3               2               NaN     600.0
2   29/10/20    600        600          3               3               800.0   800.0
4   30/10/20    200        800          1               3               250.0   NaN

(旧答案)

只需使用.sort_values()进行排序,并使用.shift与下一行进行比较

data = [['29/10/20', 400, 300, 2, 1], 
       ['29/10/20', 250, 400, 1, 2], 
       ['29/10/20', 600, 600, 3, 3],
       ['30/10/20', 800, 500, 3, 2],
       ['30/10/20', 200, 800, 1, 3], 
       ['30/10/20', 550, 300, 2, 1]]

df = pd.DataFrame(data, columns = ['date', 'east_price', 'west_price', 'east_position', 'west_position'])

解决方案#1

df = df.sort_values('east_price')
df['nearest_east_price'] = df.shift(-1)['east_price']
df = df.sort_values('west_price')
df['nearest_west_price'] = df.shift(-1)['west_price']
df

解决方案#2-更好的是,如果您有许多列的模式与其列名相似,您可以基于相似的名称在列中循环,并以这种方式执行:

for col in df.columns:
    if '_price' in col:
        df = df.sort_values(col)
        nearest_col = f'nearest_{col}'
        df[nearest_col] = df.shift(-1)[col]

解决方案#3:整合解决方案#2:

for col in df.columns:
    if '_price' in col: df[f'nearest_{col}'] = df.sort_values(col).shift(-1)[col]
df

输出:

    date        east price  west price  east position   west position   nearest_east_price  nearest_west_price
0   29/10/20    400        300          2               1               550.0   300.0
5   30/10/20    550        300          2               1               600.0   400.0
1   29/10/20    250        400          1               2               400.0   500.0
3   30/10/20    800        500          3               2               NaN     600.0
2   29/10/20    600        600          3               3               800.0   800.0
4   30/10/20    200        800          1               3               250.0   NaN

相关问题 更多 >