[英]Insert dictionary value from column according to its key into another column using pandas
I'm new to python and have been trying to face this wall for some time now.我是 python 的新手,一段时间以来一直试图面对这堵墙。 I'm using pandas data frames.
我正在使用 pandas 数据帧。
I have two columns: X and Y我有两列:X 和 Y
Situation: X is a dictionary column.情况:X 是一个字典列。
I'm trying to populate Y column with the search of a KEY value.我正在尝试通过搜索 KEY 值来填充 Y 列。
example: {'gender':'male','job':'carpenter'}示例:{'性别':'男性','工作':'木匠'}
Goal: if 'gender' KEY is equal to 'male' then populate Y column with its 'job' value (carpenter).目标:如果“性别”KEY 等于“男性”,则用其“工作”值(木匠)填充 Y 列。
thank you for your help!感谢您的帮助!
df1=pd.DataFrame({'gender':'male','job':'carpenter'},index=[0])
df1.assign(populate=np.where(df1.gender.eq('male'),df1.job,''))
out出去
gender job populate
0 male carpenter carpenter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.