[英]Assigning values to dataframe columns from a list based on value of another column in dataframe
I have two dataframes, df1 and df2, where我有两个数据帧,df1 和 df2,其中
df1 = pd.DataFrame(np.array([['a', 'b', 'c','d'], [1, 2, 3, 4]]),
columns=['x', 'y'])
df2 = pd.DataFrame(np.array([['a', 'b', 'c', 'a', 'c', 'b','b'], [4, 5, 6, 1, 32, 1, 8]]),
columns=['x', 'z'])
I want to create a dataframe df3 based on df2 with the appropriate values of df1['y'] assigned.我想创建一个基于 df2 的数据框 df3,并分配了适当的 df1['y'] 值。 So for example I would like the result to look like:
例如,我希望结果如下所示:
df3 = pd.DataFrame(np.array([['a', 'b', 'c', 'a', 'c', 'b','b'], [4, 5, 6, 1, 32, 1, 8] , [1, 2, 3, 1, 3, 2, 2 ] ]),
columns=['x', 'z', 'y'])
In my real case there are many thousand possible values of x so I would like to avoid if df3['x'] == a: df3['y'] = 1
style solutions if possible.在我的实际情况中,x 有数千个可能的值,因此
if df3['x'] == a: df3['y'] = 1
可能,我想避免if df3['x'] == a: df3['y'] = 1
样式解决方案。
您可以通过合并实现此目的:
df3 = df2.merge(df1, how="left", on="x")
Setup:设置:
import pandas as pd
import numpy as np
data={'x':['a', 'b', 'c','d'],
'y':[1, 2, 3, 4]}
df1 = pd.DataFrame(data)
data2={'x':['a', 'b', 'c', 'a', 'c', 'b','b'],
'z':[4, 5, 6, 1, 32, 1, 8]}
df2 = pd.DataFrame(data2)
data3={'x':['a', 'b', 'c', 'a', 'c', 'b','b'],
'z':[4, 5, 6, 1, 32, 1, 8]}
df3 = pd.DataFrame(data3)
Dictionary method:字典法:
# Make a dictionary
dict = df1.set_index('x').to_dict()
# Map to dictionary
df3 = df2.assign(x=df2['x'].map(*dict.values()))
You have to unzip dict
because it actually comes out as 2 dicts, but you only want the second here.您必须解压缩
dict
因为它实际上是 2 个dict
,但您只想要第二个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.