我想尝试根据来自另一个数据框的信息选择正确的索引行和列。我在数据帧(df_one)中列出了正确的索引号['index_nums'],并且在['names'],'John','Sam','Peter'等处的同一行上是它们的名称。这些名称是另一个数据帧(df_two)中的列标题。我想从df_one中说,如果'index_nums'等于df_two中的索引行,并且'names'等于df_two中的列标题。将此特定值提取到df_one的新列中。
df_one:
['Index_nums']['Names']
3 Sam
4 John
5 Steve
df_two:
['John'] ['Sam'] ['Steve']
0 23.5 45.4 52.2
1 33.3 9.9 10.2
3 22.2 9.0 7.3
4 12.5 15.5 15.4
5 24.4 8.2 4.4
6 15.5 25.1 1.9
预期产量:
df_one:
['Index_nums'] ['Names'] ['new_col']
3 Sam 9.0
4 John 12.5
5 Steve 4.4
您可以使用DataFrame.lookup
,直接对的列进行解包df_one
,以便将用作的参数lookup
,分别期望的row_labels
和序列col_labels
:
df_one['new_col'] = df_two.lookup(*df_one.values.T)
Index_nums Names new_col
0 3 Sam 9.0
1 4 John 12.5
2 5 Steve 4.4
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句