简体   繁体   English

使用 pandas 将列中的字典值根据其键插入另一列

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

相关问题 使用另一列上的键和字典中的值替换熊猫列值 - Replace pandas column values using a key on another column and value from a dictionary pandas - 用另一列替换字典列的键值 - pandas - repalce a key's value of a dictionary column with another column python-如何使用另一列中的值作为Pandas中已定义字典的键来更新列? - python- How to update a column using value in another column as key to a defined dictionary in Pandas? 熊猫-使用列作为字典的键 - pandas - using a column as a key for a dictionary Pandas:将字典列转换为键和值列 - Pandas: Dictionary column into key and value column 如何将 pandas dataframe 中的列值作为新的键值对添加到具有字典的另一列中? - How can I add column values from a pandas dataframe as a new key value pair in another column having dictionary? 如何将多键字典转换为 pandas dataframe,其中每个键和值都有自己的列? - How to convert a multi-key dictionary to a pandas dataframe, where each key and value has its own column? Pandas - 使用列中的值作为单独字典中的键 - Pandas - Use Value in Column as key in separate dictionary Pandas 用字典中的键替换列中的值 - Pandas replace value in column with key in dictionary 通过映射到包含另一列中字符串值子集的字典来创建数据框列。 作为它的关键 - Create dataframe column by mapping against a dictionary that contains a subset of string value in another column. as its key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM