For loop to make it easier - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: For loop to make it easier (/thread-24523.html) |
For loop to make it easier - Amirdst - Feb-18-2020 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 filei 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 . RE: For loop to make it easier - pyzyx3qwerty - Apr-28-2020 Please use proper code tags while posting a thread RE: For loop to make it easier - Yoriz - Apr-28-2020 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)
|