Storing csv file's contents into data Frames [Python Pandas] -


i have written function reads csv files, stores them data frame , resample them on hourly basis. below code;

def abc(path1,path2):     df1=pd.read_csv(path1, sep='\t',names = ["datetime", "value"])     df2=pd.read_csv(path2, sep='\t',names = ["datetime", "value"])     df1['datetime']=pd.to_datetime(df1['datetime'])     df1=df1.set_index('datetime')     df1=df1.resample('h',how='sum')     df2['datetime']=pd.to_datetime(df2['datetime'])     df2=df2.set_index('datetime')     df2=df2.resample('h',how='sum')     abc = pd.dataframe(df1['value'] + df2['value'])     abcd = abc * 0.519     return abc, abcd abc, abcd= abc('c:\users\desktop\b1.tsv'                              ,'c:\users\desktop\b2.tsv') 

this program works if have 30 file paths difficult make 30 data frames , process. thinking of following way of doing above;

def abc():     path= ['b1','b2','b3']     general = []     in path:         url = ('c:\users\desktop\%s.tsv'%i)         x = pd.read_csv(url,sep='\t',names = ["datetime", "value"])         x['datetime']=pd.to_datetime(x['datetime'])         x=x.set_index('datetime')         x=x.resample('h',how='sum')         general.append(x)         return general df=abc() print df 

the above code output 1 dataframe , not outputting first script doing. idea doing wrong?

you return (after first iteration). it's indentation problem. function should read:

def abc():     path= ['b1','b2','b3']     general = pd.dataframe()     in path:         url = ('c:\users\desktop\%s.tsv'%i)         x = pd.read_csv(url,sep='\t',names = ["datetime", "value"])         x['datetime']=pd.to_datetime(x['datetime'])         x=x.set_index('datetime')         x=x.resample('h',how='sum')         if len(general) == 0:             general = x         else:             general = general['values'] + x['values']     return general 

notice indentation of last line.


edit: added code sum dataframes inside loop requested in comment.

first, create empty dataframe called general. @ first iteration (when length of empty dataframe 0), assign current dataframe x general. @ subsequent iterations, add values of current dataframe sum of previous dataframes' values, stored in general.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -