我试图映射我DataFrame中一列的值,但是仅在满足另一列条件的情况下。要选择要映射的行,我可以简单地使用.loc,然后将其修改为:
data = pd.DataFrame({'col_1': [1,2,2,3,1,2,3,2], 'col_2':[100, 'information/string', 'information/string', 4, 600, 'information/string', 7, 'information/string']})
relevant_rows = data.loc[data['col_1']==2]
relevant_rows = data.apply(lambda x : x.split('/')[0] if '/' in x else x)
问题是如何将related_data数据框与原始数据框结合在一起?我这样做的尝试是:
data.loc[data['col_1']==2] = relevant_rows
但是这不起作用,我认为由于此处使用的.loc []运算符返回DataFrame的副本,而不是DataFrame本身...
有没有一种快速的方法来实现我所需要的?
我要编辑的DataFrame如下:
col_1 col_2
1 100
2 information/string
2 information/string
3 4
1 600
2 information/string
3 7
2 information/string
我的目标是:
col_1 col_2
1 100
2 information
2 information
3 4
1 600
2 information
3 7
2 information
即在col_2中有字符串的所有行都将被编辑。
答案 0 :(得分:1)
看起来您可以做到:
df['relevant_rows'] = df['col_2'].apply(lambda x : x.split('/')[0] if '/' in x else x)
答案 1 :(得分:1)
这对我有用
.main-header