我有一个大约有3000个条目和10列的数据集,所以这是一个简单得多的版本
df =
asset tail more_info
0 x a this is a long text field that is right
1 x b this is a long text field that is almost right
2 y a this is right
3 y b this is probably not right
期望的结果
df =
asset tail more_info
0 x a this is a long text field that is right
1 x b this is a long text field that is right
2 y a this is right
3 y b this is right
所以我正在尝试更新我的more_info字段,其中资产匹配并且尾部等于'a‘实际上数据集更复杂,所以我需要通过编程来完成,这就是我在该逻辑中空白的地方
def my_func(x):
if x.asset == x.asset and x.tail =='b':
'''
this would be where I'd set it to x.more_info where tail = 'a' maybe numpy where ??
'''
df['more_info'] = df['more_info'].apply(lambda x: my_func(x))
发布于 2021-05-07 01:54:42
您可以尝试这样做:
# reduce dataframe to contain only
df1 = df[['asset', 'tail']].copy()
# slice df to get only the ones you want to use for "more_info"
df2 = df[df['tail']=='a'][['asset', 'more_info']].copy()
df1.merge(df2, on=['asset'])
# asset tail more_info
# 0 x a this_is_a_long_text_field_that_is_right
# 1 x b this_is_a_long_text_field_that_is_right
# 2 y a this_is_right
# 3 y b this_is_right
或者一行:
df[['asset', 'tail']].merge(df[df['tail']=='a'][['asset', 'more_info']], on=['asset'])
https://stackoverflow.com/questions/67423340
复制相似问题