6

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')
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
DanZimmerman
  • 1,626
  • 6
  • 23
  • 45
  • The code you've included runs without error. Can you please include the part of your code that is throwing the error? But the issue is almost certainly that you're calling `to_csv` on a `Styler` object instead of a `DataFrame`. – Alex Alifimoff Jun 27 '17 at 20:34
  • I've edited my post. – DanZimmerman Jun 27 '17 at 20:37
  • What version of Python are you using? Can you post the full traceback? This again runs on my system without error (and outputs aaa.csv) – Alex Alifimoff Jun 27 '17 at 20:39
  • I forgot to add "df = df.style.format(formatter)". The reason why you had no error was the df has not been altered. Now the df is a Styler object, and when exporting to csv it will give us error. Can you try again? – DanZimmerman Jun 27 '17 at 20:41
  • So, I believe the issue is that you're overwriting your data frame, as the style application should be inplace. However, I'm not sure pandas styles work with anything except the HTML representation. – Alex Alifimoff Jun 27 '17 at 22:36
  • @AlexAlifimoff you are right. That's the same thing that I got so far regarding pandas styler objects. Not sure if there is a way to solve this issue. – DanZimmerman Jun 27 '17 at 22:50
  • @AlexAlifimoff It looks like the openpyxl engine can do the work: https://pandas.pydata.org/pandas-docs/stable/style.html – DanZimmerman Jun 27 '17 at 22:54
  • The other option would be to just change the columns to strings (formatted as you desire) and then export to CSV. – Alex Alifimoff Jun 28 '17 at 00:58

3 Answers3

5

Well, it is because CSV is a plain text format and these files don't contain styling (formatting) information. That's why you are getting AttributeError: 'Styler' object has no attribute 'to_csv'. You need to save it as an excel file to use different stylings.

MehmedB
  • 1,059
  • 1
  • 16
  • 42
1

I also face same problem and it solved by flowing this
At first import

pip install openpyxl

then save your file as

df.to_excel("output.xlsx")
Royel
  • 11
  • 4
0

Not using to_csv, but what you want:

from pathlib import Path

s = df.style.hide(axis='index').format(formatter)

path = Path('aaa.csv')
path.write_text(','.join(df.columns))
path.write_text(s.to_string(delimiter=','))

Please / comment here to get to_csv on styler.

davetapley
  • 17,000
  • 12
  • 60
  • 86