简体   繁体   English

熊猫根据索引向“数据框”列添加值,如果值存在,则追加

[英]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.

相关问题 Python Pandas dataframe - 根据索引值添加新列 - Python Pandas dataframe - add a new column based on index value Pandas - 检查列中的值是否存在于 MultiIndex 数据帧的任何索引中 - Pandas - Check if value from a column exists in any index of a MultiIndex dataframe Pandas - 根据与数据框中某个值匹配的系列索引,将系列中的值添加到数据框列 - Pandas - Add values from series to dataframe column based on index of series matching some value in dataframe 根据列值而不是索引值从pandas数据框中排除行 - excluding rows from a pandas dataframe based on column value and not index value 根据索引使Pandas Dataframe列等于另一个Dataframe中的值 - Make Pandas Dataframe column equal to value in another Dataframe based on index 检查熊猫数据帧索引中是否存在值 - Check if a value exists in pandas dataframe index 如何根据其他列值重命名Pandas DataFrame索引标签 - How to rename a pandas DataFrame index label based on other column value Pandas DataFrame根据列,索引值比较更改值 - Pandas DataFrame change a value based on column, index values comparison 根据索引,列名和原始值映射Pandas数据帧? - Map Pandas dataframe based on index, column name and original value? select 列值基于 Pandas DataFrame 中的索引名称 - select column value based on index name in Pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM