英文:
How do I filter columns in a dataframe by the keys in another dataframe
问题
我的第一个数据框架是
obj1 obj2 obj3
01 02 03
02 03 04
另一个数据框架是:
col1 col2 col3
obj1 M N
obj2 M T
obj3 M N
我只想要第一个数据框架中被标记为
{'col2':M, 'col3':N}
的列。
所需的结果是
obj1 obj3
01 03
02 04
我似乎无法以合适的方式实现这一点。
英文:
My first dataframe is
obj1 obj2 obj3
01 02 03
02 03 04
another dataframe is:
col1 col2 col3
obj1 M N
obj2 M T
obj3 M N
I only want the columns in the first dataframe who are marked with
{'col2':M, 'col3':N}
The wanted result is
obj1 obj3
01 03
02 04
I can't seem to achieve this in a decent way.
答案1
得分: 2
以下是翻译好的部分:
一种方法是将字典转换为一行DataFrame
,然后使用merge
函数进行合并,因为没有指定on
参数,它将根据字典键的所有列名的交集进行合并(在辅助DataFrame中):
d = {'col2':'M', 'col3':'N'}
vals = pd.DataFrame([d]).merge(df2)['col1']
或者使用字典推导式和DataFrame.query
:
vals = df2.query(' & '.join(['{}=={}'.format(i,repr(j)) for i, j in d.items()]))['col1']
然后通过Index.isin
和DataFrame.loc
进行筛选,:
表示所有行:
df = df1.loc[:, df1.columns.isin(vals)]
print (df)
obj1 obj3
0 01 03
1 02 04
英文:
One idea is convert dictionary to one row DataFrame
and merge
, because no parameter on
it merging by intersection of all columns names (in helper DataFrame by keys of dictionary):
d = {'col2':'M', 'col3':'N'}
vals = pd.DataFrame([d]).merge(df2)['col1']
Or use DataFrame.query
with dict comprehension:
vals = df2.query(' & '.join(['{}=={}'.format(i,repr(j))
for i, j in d.items()]))['col1']
And then filter by Index.isin
and DataFrame.loc
, :
means all rows:
df = df1.loc[:, df1.columns.isin(vals)]
print (df)
obj1 obj3
0 01 03
1 02 04
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论