如何合并熊猫数据框value_counts的输出

问题描述 投票:0回答:1

如何合并熊猫数据框value_counts的输出?

我有一个100列以上的pandas数据框。如果我执行:

$ #df = dataframe with 100+ columns

$ df_names_all = df.name.value_counts()
$ df_names_all 
# notice: name and value only
output:
Bob   100
Sally 200
Rufus 300

# then apply a filter condition
$ df_filtered = df.loc[(df.some_column == some_value)]

$ df_names_filtered = df_filtered.name.value_counts()
$ df_names_filtered 
#notice name and value only
output:
Bob   50
Sally 60
Rufus 80

Problem #1:
If I merge or join df_names_all and df_names_filtered, I get a result that is 100+ columns of 'not what I wanted'

Problem #2:
**What I want** is one dataframe with three columns
output:
Bob   100 50
Sally 200 60
Rufus 300 80

我如何最好用一行代码合并两个输出并获得上述结果?另外,我真的需要将输出与原始数据集断开连接,以免将100+列合并到答案中。

pandas dataframe merge
1个回答
0
投票

使初始value_counts像这样的数据帧:

$ df_names_all= pd.DataFrame(df.name.value_counts())

然后,当您进行第二次操作时,使其成为上方框架的一列:

$ df_names_all['Filtered'] = df.loc[(df.some_column == some_value)].value_counts()
© www.soinside.com 2019 - 2024. All rights reserved.