[英]Pandas Add value to Dataframe column based on index and if value exists then append
I'm assigning value to column based on index value and if value exists then append the new value to old value 我根据索引值将值分配给列,如果值存在,则将新值附加到旧值
I have tried applying value to column based on index but couldn't append value to already existing value in column 我尝试根据索引将值应用于列,但无法将值追加到列中已存在的值
Dataframe:- 数据帧: -
foo bar
0 1 1
1 2 2
2 3 3
3 3 3
Code :- 代码:-
values = ['a', 'b']
for val in values:
df.loc[df['foo'] == 1, foo2] = val
Outupt Dataframe: Outupt数据框:
foo bar foo2
0 1 1 b
1 2 2
2 3 3
3 3 3
Expected DataFrame:- 预期的DataFrame:
foo bar foo2
0 1 1 a,b
1 2 2
2 3 3
3 3 3
As you noticed the line 正如您所注意到的
df.loc[df['foo'] == 1, foo2] = b
overwrites previous value so that you end up with only b
. 覆盖先前的值,以便最终只包含
b
。 Is there a particular reason you need to solve your problem this way? 您是否需要通过这种特定方式解决问题? You can write a custom function and use apply to append values but this is not how pandas is supposed to be used.
您可以编写一个自定义函数,并使用apply附加值,但这不是应该使用pandas的方式。 I would recommend creating another column
'foo3'
. 我建议创建另一个列
'foo3'
。 You can always join the letters afterwards. 之后,您始终可以加入字母。
This should achieve what you are looking for. 这应该可以实现您想要的。 However, just as maow already pointed out, you should think about adding new columns and concatenating them when you need to.
但是,正如maow已经指出的那样,您应该考虑添加新列,并在需要时将它们串联起来。
import pandas as pd
df = pd.DataFrame({'foo': [1,2,3,3], 'bar': [1,2,3,3], 'foo2':[None, None, None, None]})
print(df)
a = 'a'
b = 'b'
# insert a
df.loc[df['foo'] == 1, 'foo2'] = a
print(df)
# concat b to a
df.loc[df['foo'] == 1, 'foo2'] += ',' + b
print(df)
This should solve your problem 这应该可以解决您的问题
import pandas as pd
df = pd.DataFrame({'foo': [1,2,3,3], 'bar': [1,2,3,3], 'foo2':[None, None, None, None]})
print(df)
print(df)
values = ['a', 'b']
for val in values:
print(df.loc[df['foo'] == 1,'foo2'][0])
if (df.loc[df['foo'] == 1,'foo2'][0]==None):
df.loc[df['foo'] == 1, 'foo2']=val
else:
df.loc[df['foo'] == 1, 'foo2']=df.loc[df['foo'] == 1:,'foo2'] + ',' +val
print(df)
output: 输出:
foo bar foo2
0 1 1 a,b
1 2 2
2 3 3
3 3 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.