How to collapse/group columns in pandas

Question:

I have the data with column names as days up to 3000 columns with values 0/1, ex;
sample data

And would like to convert/group the columns as weekly (1-7 in week_1 & 8-14 in week_2), ex;
output data

if the columns between 1-7 has at least 1 then week_1 should return 1 else 0.

Asked By: Jayahe

||

Answers:

Convert first column to index and then aggregate max by helper array created by integer division of 7 and added 1:

pd.options.display.max_columns = 30

np.random.seed(2020)
df = pd.DataFrame(np.random.choice([1,0], size=(5, 21), p=(0.1, 0.9)))
df.columns += 1
df.insert(0, 'id', 1000 + df.index)
print (df)
     id  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  
0  1000  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   
1  1001  0  0  0  0  0  1  0  0  0   0   0   0   0   0   0   0   0   0   0   
2  1002  0  0  1  0  0  0  0  0  0   0   0   0   0   1   0   0   0   0   0   
3  1003  0  0  1  0  0  0  0  0  0   0   1   0   0   0   0   1   0   0   0   
4  1004  0  1  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   

   20  21  
0   0   0  
1   0   0  
2   0   0  
3   0   0  
4   0   0  

df = df.set_index('id')
arr = np.arange(len(df.columns)) // 7 + 1
df = df.groupby(arr, axis=1).max().add_prefix('week_').reset_index()

print (df)
     id  week_1  week_2  week_3
0  1000       0       0       0
1  1001       1       0       0
2  1002       1       1       0
3  1003       1       1       1
4  1004       1       0       0
Answered By: jezrael
import pandas as pd
import numpy as np

id = list(range(1000, 1010))
cl = list(range(1,22))
data_ = np.random.rand(10,21)
data_
client_data = pd.DataFrame(data=data_, index=id, columns=cl)


def change_col(col_hd=int):
    week_num = (col_hd + 6) // 7
    week_header = 'week_' + str(week_num)
    return week_header


new_col_header = []
for c in cl:
    new_col_header.append(change_col(c))

client_data.columns = new_col_header

client_data.columns.name = 'id'

client_data.groupby(axis='columns', level=0).sum()
Answered By: Bhosale Shrikant
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.