'How to make python go through all csv files, perform edit on every one of them, and save them seperatly?
I'm working with many csv files that contain stimulus timing. My input files look like this:
filename,onset condition,duration condition,choice,walking,biking,driving,swimming,boating,climbing
0212_indoor.jpg,8.000757300000032,1,0,1,0,0,0,0,0
0017_outdoor_natural.jpg,12.817914299999984,1,0,1,0,0,0,0,1
0165_outdoor_manmade.jpg,16.01749719999998,1,0,1,0,0,0,0,0
0216_indoor.jpg,19.21756840000012,1,0,1,0,0,0,0,0
0021_indoor.jpg,22.41756989999999,1,0,1,0,0,0,0,0
My script currently takes this input and generates a new ordering of the files (where boating/swimming/driving etc. are translated to individual condition names). Currently my code looks as follows:
import pandas as pd
import os
from glob
name = input("Enter file number : ") + '.csv'
df = pd.read_csv(name)
def condition (row):
if row['choice'] > 0.9 :
return 'Action_trial'
if row['walking'] + row['biking'] + row['driving'] + row['climbing'] > 3 :
return '1' #WBiDC
if row['walking'] + row['biking'] + row['driving'] > 2 :
return '2' #'WBiD'
if row['walking'] + row['biking'] > 1 :
return '3'#'WBi'
if row['biking'] + row['driving'] > 1 :
return '4' #'BiD'
if row['walking'] + row['driving'] > 1 :
return '5' #'WD'
if row['walking'] + row['climbing'] > 1 :
return '6' #'WC'
if row['swimming'] + row['boating'] > 1 :
return '7' #'SBo'
if row['walking'] == 1 :
return '8' #'W'
if row['biking'] == 1 :
return '9' #'Bi'
if row['driving'] == 1 :
return '10' #'D'
if row['climbing'] == 1 :
return '11' #'C'
if row['swimming'] == 1 :
return '12' #'S'
if row['boating'] == 1:
return '13' #'B'
if row['climing'] == 1:
return '14' #'C'
return 'Other'
df['conditions'] = df.apply (lambda row: condition(row), axis=1)
df2 = df[['onset condition', 'duration condition', 'conditions']]
file = input('Enter file output name: ')
df2.to_csv(file+'.csv', sep ='\t', header=False, index=False)
Which generates the following output file:
"8.000757300000032 1 W"
"12.817914299999984 1 WC"
"16.01749719999998 1 W"
"19.217568400000122 1 W"
"22.41756989999999 1 W"
"25.617656800000077 1 B"
"30.418041300000137 1 D"
"35.218208600000025 1 S"
What I need is to have a script that automatically loops through all the .csv files in a directory, performs the computation achieved in the above and stores the new .csv (automatically naming them).
If any of you could help me with this, thanks a lot!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
