[英]Extract row data from dictionary if dataframes based on filter on a column value
The dictionary dict_set has dataframes as the value for their keys.字典 dict_set 将数据帧作为其键的值。
I'm trying to extract data from a dictionary of dataframes based on a filter on 'A' column in the dataframe based on the value in column.我正在尝试基于列中的值基于数据框中“A”列上的过滤器从数据框字典中提取数据。
dict_set={}
dict_set['a']=pd.DataFrame({'A':[1,2,3],'B':[1,2,3]})
dict_set['b']=pd.DataFrame({'A':[1,4,5],'B':[1,5,6]})
df=pd.concat([dict_set[x][dict_set[x]['A']==1] for x in dict_set.keys()],axis=0)
output being the below.输出如下。
A B
0 1 1
0 1 1
But I would want the output to be但我希望输出是
A B x
0 1 1 a
0 1 1 b
Basically, I want the value of x to be present in the new dataframe formed as a column, say column x in the dataframe formed such that df[x] would give me the x values.基本上,我希望 x 的值出现在作为列形成的新数据帧中,比如形成的数据帧中的 x 列,这样 df[x] 就会给我 x 值。 Is there a simple way to do this?
有没有一种简单的方法可以做到这一点?
Try this:尝试这个:
pd.concat([df.query("A == 1") for df in dict_set.values()], keys=dict_set.keys())\
.reset_index(level=0)\
.rename(columns={'level_0':'x'})
Output:输出:
x A B
0 a 1 1
0 b 1 1
Details:细节:
Let's get the dataframes from the dictionary using list comprehension and filter the datafames.让我们使用列表理解从字典中获取数据帧并过滤数据名。 Here, I choose to use
query
, but you could use boolean index with df[df['A'] == 1]
also, then pd.concat
with the keys
parameter set to the dictionary keys.在这里,我选择使用
query
,但您也可以使用带有df[df['A'] == 1]
布尔索引,然后pd.concat
将keys
参数设置为字典键。 Lastly, reset_index
level=0 and rename
.最后,
reset_index
level=0 rename
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.