python - 根据条件重复数据帧行

标签 python pandas dataframe

我正在寻找一种根据值条件插入重复行的方法。

输入数据集包含客户价格和价格有效期(以周为单位) - 'price_start_week''price_end_week'
这个想法是通过添加包含实际周的新列来扩展数据框,并根据有效周数重复行。

输入:

╔═══════════════╦══════════════════╦════════════════╦═════════════╗
║ customer_name ║ price_start_week ║ price_end_week ║ price_value ║
╠═══════════════╬══════════════════╬════════════════╬═════════════╣
║ A             ║                4 ║              7 ║         500 ║
║ B             ║                3 ║              6 ║         600 ║
║ C             ║                2 ║              4 ║         700 ║
╚═══════════════╩══════════════════╩════════════════╩═════════════╝

输出:

+---------------+------------------+----------------+-------------+-------------+
| customer_name | price_start_week | price_end_week | actual week | price_value |
+---------------+------------------+----------------+-------------+-------------+
| A             |                4 |              7 |           4 |         500 |
| A             |                4 |              7 |           5 |         500 |
| A             |                4 |              7 |           6 |         500 |
| A             |                4 |              7 |           7 |         500 |
| B             |                3 |              6 |           3 |         600 |
| B             |                3 |              6 |           4 |         600 |
| B             |                3 |              6 |           5 |         600 |
| B             |                3 |              6 |           6 |         600 |
| C             |                2 |              2 |           4 |         700 |
| C             |                2 |              3 |           4 |         700 |
| C             |                2 |              4 |           4 |         700 |
+---------------+------------------+----------------+-------------+-------------+

最好的方法是什么?

我正在考虑应用函数,例如:

def repeat(a):
    if (a['price_start_week']>a['price_end_week']):
        return a['price_start_week']-a['price_end_week']
    ...
df['actual_week']=df.apply(repeat, axis=0)

最佳答案

使用Index.repeat按周与然后之间的差异 GroupBy.cumcount每组计数:

a = df['price_end_week'] - df['price_start_week'] + 1
df = df.loc[df.index.repeat(a)].reset_index(drop=True)
df['actual week'] = df.groupby('customer_name').cumcount() + df['price_start_week']
print (df)
   customer_name  price_start_week  price_end_week  price_value  actual week
0              A                 4               7          500            4
1              A                 4               7          500            5
2              A                 4               7          500            6
3              A                 4               7          500            7
4              B                 3               6          600            3
5              B                 3               6          600            4
6              B                 3               6          600            5
7              B                 3               6          600            6
8              C                 2               4          700            2
9              C                 2               4          700            3
10             C                 2               4          700            4

关于python - 根据条件重复数据帧行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51243444/

相关文章:

python - Socket.io python 服务器

python - 如何减去 Pandas Dataframe 中的两个日期时间值

python - 如何通过多列集过滤 Pandas 数据框?

python - scatter 的 'numpy.float64' 属性收到的类型 'y' 的值无效

python - 当设置 rlim 时,极坐标条形图上的条形被切断

python - 将列表的某些字符转换为字符串

python - 在 Dataframe 中的特定位置添加列

python - 如何动态链接 Pyspark 中的条件?

python - Pandas 在 groupby 层次结构上应用样式

python - 将某些列除以 Pandas 中的另一列