I have dataframe like this
A B
0 1 a
1 2 a
2 3 b
3 4 b
4 5 a
I want to get result below(1row*4column dataframe),
A_count_all means the number of rows in dataframe df.A.count()
A_sum_all means the df.A.sum()
A_count_a is df.loc[df.B==a,"A"].count()
A_sum_a is df.loc[df.B==a,"A"].sum()
A_count_all A_sum_all A_count_a A_sum_a
0 5 15 3 8
how can I get this result dataframe?
You can use DataFrame
constructor:
A_count_all = df.A.count()
A_sum_all = df.A.sum()
A_count_a = df.loc[df.B=='a',"A"].count()
A_sum_a = df.loc[df.B=='a',"A"].sum()
print (pd.DataFrame({'A_count_all':A_count_all,
'A_sum_all':A_sum_all,
'A_count_a':A_count_a,
'A_sum_a':A_sum_a},
index=[0],
columns=['A_count_all','A_sum_all','A_count_a','A_sum_a']))
A_count_all A_sum_all A_count_a A_sum_a
0 5 15 3 8
Thank you Kris
for another solution:
print (pd.DataFrame(data=[[df.A.count(),
df.A.sum(),
df.loc[df.B=='a',"A"].count(),
df.loc[df.B=='a',"A"].sum()]],
columns=['A_count_all','A_sum_all','A_count_a','A_sum_a']))
A_count_all A_sum_all A_count_a A_sum_a
0 5 15 3 8
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.