[英]Extracting specific column values in pandas DataFrame comparing another DataFrame
我有以下 pd 框架:
Name Age index_col
0 tom 10 1
1 nick 15 2
2 juli 14 3
另外,我有這樣的第二個 pd 框架:
Name
0 tom
1 juli
現在,我想從第一個pd 幀中提取第二個 pd 幀的“ index_col ”信息。 我的意思是 output 應該是 [1,3]。 output 格式並不重要。 它可以是列表、numpy 數組、pd 框架等。如何在不使用循環的情況下做到這一點?
謝謝。
讓我們試試
df1.loc[df1.Name.isin(df2.Name),'index_col']
或者如果訂單很重要
df.iloc[pd.Index(df1.Name).get_indexer(df2.Name)]['index_col']
Out[103]:
0 1
2 3
Name: index_col, dtype: int64
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.