我正在为这个拔头发。帮助表示赞赏。
我有一个我正在处理的数据框,它部分涉及将驻留在多行上的数据合并为一行。我正在尝试使用 df.loc 来做到这一点:
df.loc[df['foo'] == 1, 'Output Column'] = df.loc[df['bar'] == 2, 'Desired Column']
所以我想要的是对于 'foo' = 1 的任何行,去查找 'bar' = 2 的位置并将驻留在 'Desired Column' 中的值放入原始行。本质上,这将合并行以创建更清晰的输出。以玩具为例...
(编辑以显示我的代码出错的地方)这是我想要的......之前:
idx foo bar Desired Column Output Column
0 1
1 2 Hi there!
2 1
3 6
后:
idx foo bar Desired Column Output Column
0 1 Hi there!
1 2 Hi there!
2 1 Hi there!
3 6
然而,这是我实际得到的:之前:
idx foo bar Desired Column Output Column
0 1
1 2 Hi there!
2 1
3 6
后:
idx foo bar Desired Column Output Column
0 1
1 2 Hi there! Hi there!
2 1
3 6
谢谢你的帮助!
牧羊人nacy
波斯汪
相关分类