[英]Mapping Values of key in Dictionary present in dataframe columns
I have a dataframe A with column 'col_1' and values of column is A and B and and I am trying to map the values of A and B present in Dictionary我有一个 dataframe A 列 'col_1' 并且列的值为 A 和 B 并且我正在尝试 map 字典中存在的 A 和 B 的值
DataFrame A: DataFrame
enter image description here在此处输入图像描述
and have dictionary并有字典
enter image description here在此处输入图像描述
and I want the output like this我想要这样的 output
Dataframe: Dataframe:
col_1 Values col_1 值
Any help will be highly appreciated thanks任何帮助将不胜感激谢谢
I tried to frame your problem properly:我试图正确地描述你的问题:
df = pd.DataFrame({"col_1":["A","A","A","B","B"]})
Printing df
gives us your dataframe
shown in the image above:打印df
会给我们您的dataframe
,如上图所示:
print(df)
col_1
0 A
1 A
2 A
3 B
4 B
Here is your dictionary:这是你的字典:
dict1 = {"A":[1,2,3], "B":[1,2]}
I created an empty list
to hold the elements then stack up the list
with your request, and finally created a new column
called values
and write the list
into the column
我创建了一个空list
来保存元素,然后根据您的请求堆叠list
,最后创建一个名为values
的新column
并将list
写入column
values1 = []
for key,value_list in dict1.items():
for item in value_list:
value_item = key+" "+ str(item)
values1.append(value_item)
df["values"] = values1
printing df
results into:将df
结果打印到:
df
col_1 values
0 A A 1
1 A A 2
2 A A 3
3 B B 1
4 B B 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.