Reputation: 1104
I have a dataframe ,how can I get column sum for every K cloumns for example, from
Name 1 2 3 4
A 2 3 5 8
B 1 2 3 4
C 1 6 8 9
D 2 4 5 5
E 3 3 3 3
summing for every 2 columns to
Name 1 2
A 5 13
B 3 7
C 7 17
D 6 10
E 6 6
The window and number of columns can be very large. Is there any method to do that without the fot loop?
Upvotes: 1
Views: 121
Reputation: 20669
You can use df.rolling
over axis 1 with window size 2.
df.set_index('Name').rolling(2,axis=1).sum().iloc[:,1::2]
2 4
Name
A 5.0 13.0
B 3.0 7.0
C 7.0 17.0
D 6.0 10.0
E 6.0 6.0
Upvotes: 2
Reputation: 862591
You can use integer division by //
with helper array by length of columns and pass to groupby
by axis=1
with aggregate sum
:
#if Name is column
df = df.set_index('Name')
K = 2
df = df.groupby(np.arange(len(df.columns)) // K, axis=1).sum()
print (df)
0 1
Name
A 5 13
B 3 7
C 7 17
D 6 10
E 6 6
Details:
print (np.arange(len(df.columns)))
[0 1 2 3]
print (np.arange(len(df.columns)) // K)
[0 0 1 1]
Or if values are consecutive integers starting by 1
is possible subtract 1
and divide:
df = df.groupby((df.columns - 1) // K, axis=1).sum()
print (df)
0 1
Name
A 5 13
B 3 7
C 7 17
D 6 10
E 6 6
Upvotes: 1