早上好,
我有以下数据框:
Owner Areas Title DNS
0 Brian Production MacOS libre
1 Brian Testing MS noapp
2 Andy Uat Unix high
3 Paul Research Linux lowhw
由第一次打印生成。 (print(df))
import pandas as pd
df = pd.DataFrame ({
'Owner':['Brian','Brian','Andy','Paul'],
'Areas':['Production','Testing','Uat','Research'],
'Title':['MacOS','MS','Unix','Linux'],
'DNS':['libre','noapp','high','lowhw']
})
print(df)
result = pd.DataFrame(df.groupby(['Owner','Areas','Title','DNS'])['Title'].count())
print(result)
名为result的新df应该如下所示:
Count_of_title
Andy 3
Uat 1
Unix 1
high 1
Brian 6
Production 1
MacOS 1
libre 1
Testing 1
Ms 1
noapp 1
Paul 3
Research 1
Linux 1
lowhw 1
Grand total 12
但是我得到了
Title
Owner Areas Title DNS
Andy Uat Unix high 1
Brian Production MacOS libre 1
Testing MS noapp 1
Paul Research Linux lowhw 1
我想要实现的是为每个所有者计算多少个Areas&Title&DNS,并在他的名字之后(Count_of_title列)写入该值。 “总计”计算得出3(来自Andy)+ 6(来自Brian)+ 3(来自Paul)= 12。 这可能吗?我也尝试过使用pivot_table / pivot,但是我没有运气。谢谢!
答案 0 :(得分:0)
我认为这就是您要寻找的
new_df = df.groupby('Owner').agg({'Areas': 'nunique', 'Title': 'nunique', 'DNS': 'nunique'})
每个所有者的总数:
new_df['Total'] = new_df['Areas'] + new_df['Title'] + new_df['DNS']
输出:
Areas Title DNS Total
Owner
Andy 1 1 1 3
Brian 2 2 2 6
Paul 1 1 1 3
总计:
Grand_Total = sum(new_df['Total'])