[英]Keep one column in df2 based on value in df1
Suppose I have two dataframes:假设我有两个数据框:
data = {'model': ['B']}
df1 = pd.DataFrame (data, columns = ['model'])
df1
data = {'A': ['0.55', '0.49', '0.47',],
'B': ['0.48', '0.53', '0.54'],
'C': ['0.50', '0.51', '0.45']}
df2 = pd.DataFrame (data, columns = ['A', 'B', 'C'])
df2
I would like df2 to keep only the column where the name is equal to the value in df1.我希望 df2 只保留名称等于 df1 中的值的列。 So the output would be that df2 only has one column in the dataframe - 'B'所以 output 将是 df2 在 dataframe - 'B' 中只有一列
How would I do this?我该怎么做?
Thanks谢谢
You can do:你可以做:
import pandas as pd
data = {'model': ['B']}
df1 = pd.DataFrame (data, columns = ['model'])
data = {'A': ['0.55', '0.49', '0.47',],
'B': ['0.48', '0.53', '0.54'],
'C': ['0.50', '0.51', '0.45']}
df2 = pd.DataFrame (data, columns = ['A', 'B', 'C'])
outdf = df2[df1['model']] # alternatively: outdf = df2[df1.model]
print(outdf)
Output: Output:
B
0 0.48
1 0.53
2 0.54
Explanation: we can use []
(getitem) to get certain columns from pandas.DataFrame
by using iterable - in this case it is pandas.Series
(column) from df1
named model
. Explanation: we can use []
(getitem) to get certain columns from pandas.DataFrame
by using iterable - in this case it is pandas.Series
(column) from df1
named model
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.