Python,通过列复制数据帧中的行';s整数并相应地减小它
我有一个pandas数据框,我想复制其中一列中超过一行的所有行,对于已复制的项,将该列中的数量减少一行。 数据帧:Python,通过列复制数据帧中的行';s整数并相应地减小它,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个pandas数据框,我想复制其中一列中超过一行的所有行,对于已复制的项,将该列中的数量减少一行。 数据帧: Item Weight Bags Must quantity must quantity bags column length assigned bag 0 planes bag 8.50 planes v 1 1 6 None 1
Item Weight Bags Must quantity must quantity bags column length assigned bag
0 planes bag 8.50 planes v 1 1 6 None
1 Full Bandolera 3.76 planes v 3 2 6 None
2 tail 0.30 planes <NA> 3 2 6 None
3 central wing 1.08 planes <NA> 3 2 6 None
4 engine 0.44 planes <NA> 3 2 6 None
5 height steer 0.12 planes <NA> 3 2 6 None
6 dihedral 0.40 planes <NA> 3 2 6 None
7 pods bag 8.72 pods v 1 1 4 None
8 Pod 1.74 pods v 3 2 4 None
9 optic 0.86 pods v 2 2 4 None
10 thermal 1.20 pods v 3 2 4 None
这里不需要按每行循环,解决方案是简化的,因为如果重复后
数量为1
,则返回同一行:
def multiply_row(df)
return df.loc[df.index.repeat(df.quantity)].assign(quantity=1).reset_index(drop=True)
df1 = multiply_row(df)
def multiply_row(df)
return df.loc[df.index.repeat(df.quantity)].assign(quantity=1).reset_index(drop=True)
df1 = multiply_row(df)