2

I am relatively new to Python (most of my experience has been in SAS), so please bear with me.

I'm trying to create a number of CSVs from an existing dataset and export them based on a defined list. The naming of the CSVs should be dynamic based on the corresponding list value.

I've tried a lot of things - mostly stabbing in the dark - and nothing works. See code below

cc = ['AD-1','AD-2','AD-3'] #the list I want it to cycle through
for index in range(len(cc)): 
    df1_cc = df[df['charge'].isin(cc)] #df is predefined 
    #set "charge" as the index variable so you can aggregate on it           
    df1_cc = df1_cc.set_index('charge')
    df1_cc
    #sum up values based on individual values of 'charge'
    table1_cc = df1_cc.sum(level='charge')
    table1_cc
    #output to CSV
    table1_cc.to_csv(r"C:\Users\etc\table1_"+cc+".csv")

Note, the values in cc (AD-1, AD-2 and AD-3) are contained in 'charge' among others

The only error I get is here:

table1_cc.to_csv(r"C:\Users\etc\"+cc+".csv")

The error I get is: TypeError: can only concatenate str (not "list") to str

The output should be 3 files: table1_AD-1.csv, table1_AD-2.csv and table1_AD-3.csv, and each should contained summed values of each individually (again, that part works. The real issue is looping through and exporting to CSV the output for each individual value in cc).

Appreciate any help!

1
  • Welcome to Python! An "idiomatic" way to re-approach your loop would be to use the "in" operator directly on your list - cc. eg: for name_stub in cc: then use name_stub where it is needed. This would be full "pythonista" style... but also, try enumerate as suggested below as well to give you an "`" style incrementing counter inside the loop! Commented Jul 17, 2019 at 0:39

2 Answers 2

2

You need to change the last line of to_csv

cc = ['AD-1','AD-2','AD-3'] #the list I want it to cycle through
for index in range(len(cc)): 
    df1_cc = df[df['charge'].isin([cc[index]])] #df is predefined 
    #set "charge" as the index variable so you can aggregate on it           
    df1_cc = df1_cc.set_index('charge')
    df1_cc
    #sum up values based on individual values of 'charge'
    table1_cc = df1_cc.sum(level='charge')
    table1_cc
    #output to CSV
    table1_cc.to_csv(r"C:\Users\etc\table1_"+cc[index]+".csv")
Sign up to request clarification or add additional context in comments.

3 Comments

This works - mostly! When I check the output, I get three correctly named CSV files. The problem is, each contains the following: charge hrs AD-1 2638 AD-2 2581 AD-3 246 Whereas the first should be: charge hrs AD-1 2638 The second should be: charge hrs AD-2 2581 And the last should be: charge hrs AD-3 246
thanks for updating. I'm getting this error: TypeError: only list-like objects are allowed to be passed to isin(), you passed a [str]
@ZDR adding [] in that line , check it again
0

You could also iterate through your cc list like this:

cc_list = ['AD-1','AD-2','AD-3'] #the list I want it to cycle through

for index,cc in enumerate(cc_list): 
    df1_cc = df[df['charge'].isin([cc])] #df is predefined 
    #set "charge" as the index variable so you can aggregate on it           
    df1_cc = df1_cc.set_index('charge')
    df1_cc
    #sum up values based on individual values of 'charge'
    table1_cc = df1_cc.sum(level='charge')
    table1_cc
    #output to CSV
    table1_cc.to_csv(r"C:\Users\etc\table1_{}.csv".format(cc))

1 Comment

Thanks Nakor. Similar to my initial comment to above, it works but the output isn't quite right: This works - mostly! When I check the output, I get three correctly named CSV files. The problem is, each contains the following: charge hrs AD-1 2638 AD-2 2581 AD-3 246 Whereas the first should be: charge hrs AD-1 2638 The second should be: charge hrs AD-2 2581 And the last should be: charge hrs AD-3 246

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.