匹配多个列的模式我正好有下列数据框:复制的列值熊猫
import pandas as pd
import numpy as np
df = pd.DataFrame({ 'Prod1': ['10','','10','','',''],
'Prod2': ['','5','5','','','5'],
'Prod3': ['','','','8','8','8'],
'String1': ['','','','','',''],
'String2': ['','','','','',''],
'String3': ['','','','','',''],
'X1': ['x1','x2','x3','x4','x5','x6'],
'X2': ['','','y1','','','y2']
})
print(df)
Prod1 Prod2 Prod3 String1 String2 String3 X1 X2
0 10 x1
1 5 x2
2 10 5 x3 y1
3 8 x4
4 8 x5
5 5 8 x6 y2
这是一个与相关联的弦产品的示意图表;实际的字符串在列中(X1
,X2
),但根据相应产品是否具有值,它们最终应移至(String1
,String2
,String3
)。
例如: 行0
对Prod1
的值,因此x1
应该搬到String1
。 行1
的值为Prod2
,因此x2
应移至String2
。
在实际的数据集中,大多数每个Prod
都有一个String
,但在Prods中有多行可以找到多个值,并且应该填充字符串列以优先考虑左侧。最终的结果应该是这样的:
Prod1 Prod2 Prod3 String1 String2 String3 X1 X2
0 10 x1
1 5 x2
2 10 5 x3 y1
3 8 x4
4 8 x5
5 5 8 x6 y1
我思考嵌套行/列的循环,但我仍然不足够的大熊猫熟悉去解决。 非常感谢您的任何建议!
您正在寻找这样的事情:([基于在熊猫数据帧其他列的值列之间的运动行值] https://stackoverflow.com/questions/15997430/moving-row-values-columns-columns-based-on-other-column-values-in-a-pandas-dataf)? – RoyaumeIX
我在研究现有答案的过程中看到了这一点,但我无法将其应用到我的案例中(可能是因为我目前的技能) - 我现在在看温的回复。 –