一个人如何合并熊猫数据框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个以上答案中的列。
答案 0 :(得分:1)
使初始value_counts
像这样的数据帧:
$ df_names_all= pd.DataFrame(df.name.value_counts())
然后,当您进行第二次操作时,使其成为上方框架的一列:
$ df_names_all['Filtered'] = df.loc[(df.some_column == some_value)].value_counts()