Given the following data frames:
import pandas as pd
k=pd.DataFrame({'A':[1,1],'B':[3,4]})
e=pd.DataFrame({'A':[1,1],'B':[6,7]})
k
A B
0 1 3
1 1 4
e
A B
0 1 6
1 1 7
I'd like to apply a group-by sum in a loop, but doing so does not seem to modify the data frames.
Here's what I've tried:
for d in dfsout:
d=d.groupby(d.columns[0]).apply(sum)
print(d)
When I print d in the loop, it shows that the correct operation is occurring...
A B
A
1 2 7
A B
A
1 2 13
...but then when I print data frames k and e, they have not been modified.
k
A B
0 1 3
1 1 4
e
A B
0 1 6
1 1 7
Update
I also tried using it as a function (works in loop, still does not modify):
def moddf(d):
return d.groupby(d.columns[0]).apply(sum)
for d in dfsout:
d=moddf(d)
print(d)
Thanks in advance!