How Do I Add New Column That Adds And Sums Counts From Existing Column?
I have this python code: counting_bach_new = counting_bach.groupby(['User Name', 'time_diff', 'Logon Time']).size() print('\ncounting_bach_new') print(counting_bach_new) ...gettin
Solution 1:
Use DataFrame.join
with Series.reset_index
:
df = (counting_bach_new.to_frame('count')
.join((counting_bach_new.reset_index()
.groupby('User Name')
.agg(MySum=('Logon Time', 'sum'),
MyCount=('Logon Time', 'count'))), on='User Name'))
print (df)
count MySum MyCount
User Name time_diff Logon Time
122770 -132 days +21:38:00 1 1 2 2
-122 days +00:41:00 1 1 2 2
123526 -30 days +12:04:00 1 1 3 3
-29 days +16:39:00 1 1 3 3
-27 days +18:16:00 1 1 3 3
201685 -131 days +21:21:00 1 1 1 1
202047 -106 days +10:14:00 1 1 1 1
202076 -132 days +10:22:00 1 1 3 3
-132 days +14:46:00 1 1 3 3
-131 days +21:21:00 1 1 3 3
Post a Comment for "How Do I Add New Column That Adds And Sums Counts From Existing Column?"