I have a dataframe that is formatted differently for each column. I need to export it to csv or dat files. But got the following error message:
AttributeError: 'Styler' object has no attribute 'to_csv'
How to solve this issue?
import pandas as pd
import datetime
def time_formatter(data):
return datetime.datetime.strptime(data, "%Y/%m/%d").date().strftime('%Y%m%d')
df = pd.DataFrame({'a':[1,2,3], 'b':['2017/01/01', '2017/01/02','2016/12/31'], 'c':['aaa', 'bbb', 'ccc'], 'd':[4,5,6]})
formatter = {'a':'{:4.2f}', 'b': time_formatter, 'd':'{:8.2f}'}
df = df.style.format(formatter)
df.to_csv('aaa.csv')
to_csv
on aStyler
object instead of aDataFrame
. – Leeland