简体   繁体   中英

Error when concatenating two groupby Dataframe in Pandas

I have a Dataframe that has daily sales data using which I created a groupby function as below:

df_new1 = df_1.groupby(['emp_id']).size() 
df_new2 = df_2.groupby(['emp_id']).size()

Sample output of df_new1:

emp_id,count1
101,2
102,4

Sample output of df_new2:

emp_id,count2
101,4
102,6

I am trying to compare the above two Dataframes ( df_new1 and df_new2 ) as below:

new_df = pd.concat(df_new1, df_new2)

This throws an error:

TypeError: first argument must be an iterable of pandas objects, you passed an object of type "Series"

Expected output:

emp_id,count1,count2
101,2,4
102,4,6

where count1 is value from df_new1 and count2 is value from df_new2

Both df_new1 and df_new2 are Series not data frames as you did not reset index after groupby. You can concat multiple series, they will get concatenated on the index. Try

new_df = pd.concat([df_new1, df_new2], axis = 1)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM