Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loop to make it easier
#1
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 .
Reply
#2
Please use proper code tags while posting a thread
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make for loop display on 1 Line Extra 3 1,374 Jan-12-2022, 09:29 PM
Last Post: Extra
  Looking for discord bot to make loop ping for address ip tinkode 0 1,788 Jul-26-2021, 03:51 PM
Last Post: tinkode
  Easier way to manage dependencies? Tylersuard 2 2,317 Jan-01-2020, 09:19 PM
Last Post: Tylersuard
  making the code easier, list comprehension go127a 2 2,017 May-26-2019, 06:19 PM
Last Post: Gribouillis
  How to make loop go back to the beginning if test evaluaes to True. FWendeburg 1 2,797 Feb-13-2019, 01:26 AM
Last Post: stullis
  How can I do it easier ? Mike Ru 7 4,877 Jul-31-2017, 05:41 PM
Last Post: wavic
  [ tools atom v pycharm ] -- when it comes to easier learning, and making life easier, oneofakindpython 2 3,324 Jun-18-2017, 08:19 AM
Last Post: 3cxmostar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020