list - Print columns of Pandas dataframe to separate files + dataframe with datetime (min/sec) -



list - Print columns of Pandas dataframe to separate files + dataframe with datetime (min/sec) -

i trying print pandas dataframe's columns separate *.csv files in python 2.7.

using code, dataframe 4 columns , index of dates:

import pandas pd import numpy np col_headers = list('abcd') dates = pd.date_range(dt.datetime.today().strftime("%m/%d/%y"),periods=rows) df2 = pd.dataframe(np.random.randn(10, 4), index=dates, columns = col_headers) df = df2.tz_localize('utc') #this not seem giving me hours/minutes/seconds

i remove index , set separate column:

df['date'] = df.index col_headers.append('date') #update column keys

at point, need print 5 columns of dataframe separate files. here have tried:

for ijk in range(0,len(col_headers)): df.to_csv('output' + str(ijk) + '.csv', columns = col_headers[ijk])

i next error message:

keyerror: "[['d', 'a', 't', 'e']] not in in [columns]"

if say:

for ijk in range(0,len(col_headers)-1):

then works, not print 'date' clumn. not want. need print date column.

questions:

how print 'dates' column *.csv file? how time hours, minutes , seconds? if number of rows changed 10 5000, seconds alter 1 row of dataframe next?

edit: - reply q2 (see here) ==> in case of particular code, see this:

dates = pd.date_range(dt.datetime.today().strftime("%m/%d/%y %h:%m"),periods=rows)

i don't quite understand logic next simpler method it:

for col in df: df[col].to_csv('output' + col + '.csv')

example:

in [41]: col in df2: print('output' + col + '.csv') outputa.csv outputb.csv outputc.csv outputd.csv outputdate.csv

list python-2.7 datetime pandas append

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -