Python Forum

Full Version: For loop to make it easier
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Friends ,
i have some aggregation to provide in xlsx files and i need to use a for loop to make it easier
i have columns in dataframe df.columns = (id ,a,b,c,d,e,f,j,h)
i want to repeat the same process for all columns using count aggregation with id
for example to obtain the dataframe aggregate_A_B the code is :

aggregate_A_B = df[['a','b']].groupby['id'].count # aggregation with id aggregate_A_B = pd.dataframe (data= aggregate_A_B)# transform to a dataframe aggregate_A_B.to_excel(aggregate_A_B.xlsx) # export to xlsx file 
i need to repeat the same to get the combination between all columns and i'm thinking about a loop for to save time
kimdly help
regards .
Please use proper code tags while posting a thread
A for loop using itertools and a itertools recipe
from itertools import tee def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = tee(iterable) next(b, None) return zip(a, b) columns = 'a', 'b', 'c', 'd', 'e', 'f', 'j', 'h' for pair in pairwise(columns): print(pair)
Output:
('a', 'b') ('b', 'c') ('c', 'd') ('d', 'e') ('e', 'f') ('f', 'j') ('j', 'h')