在Python中将给定数据框的列的选定值分配给另一个数据框

时间:2019-03-17 00:48:03

标签: python pandas dataframe

我想将一个数据帧的选定列值分配给另一个数据帧。

data = [['Math',87],['Geography',93],['Physics',72],['Geometry',75],['Astronomy',81],['English',94],['History',84]]
df = pd.DataFrame(data,columns=['Subjects','Grade'])
df

Subjects  Grade
Math         87
Geography    93
Physics      72
Geometry     75
Astronomy    81
English      94
History      84

我还有另一个数据框:

data2 = [['Astronomy'],['Geography'],['Geometry'],['History']]
df2 = pd.DataFrame(data2,columns=['Subjects_selected'])

df2

Subjects_selected
Astronomy
Geography
Geometry
History

如何将“成绩”列值自动分配给df2作为新的列元素?我希望得到:

Subjects_selected   Retrieved_Values
Astronomy                       81
Geography                       93
Geometry                        75
History                         84

2 个答案:

答案 0 :(得分:2)

Series.map创建的Series中使用DataFrame.set_index,然后选择列Grade

df2['Retrieved_Values'] = df2['Subjects_selected'].map(df.set_index('Subjects')['Grade'])
print (df2)
  Subjects_selected  Retrieved_Values
0         Astronomy                81
1         Geography                93
2          Geometry                75
3           History                84

使用DataFrame.merge并重命名列的另一种解决方案:

d = {'Subjects':'Subjects_selected','Grade':'Retrieved_Values'}
df2 = df2.merge(df.rename(columns=d), how='left')

print (df2)
  Subjects_selected  Retrieved_Values
0         Astronomy                81
1         Geography                93
2          Geometry                75
3           History                84

答案 1 :(得分:1)

自从您提到select以来,我将使用isin,它是'更喜欢选择'

df.loc[df.Subjects.isin(df2.Subjects_selected)]
Out[93]: 
    Subjects  Grade
1  Geography     93
3   Geometry     75
4  Astronomy     81
6    History     84