7

I've problem exporting a dataframe to CSV file.

Data types are String and Float64 values like this:

In [19]: segmenti_t0
Out[19]:
SEGM1  SEGM2
AD     P.S.         8.3
       SCREMATO     0.6
CRE    STD          1.2
FUN    INTERO       0.0
       P.S.         2.0
       SCREMATO     0.0
NORM   INTERO      13.1
       P.S.        69.5
       SCREMATO     5.2
Name: Quota volume_t0

I try to export this dataframe with this command:

IN [20]: segmenti_t0.to_csv('C:Users\MarioRossi\prova.csv', sep=";")

When I try to open it with Excel or I try to import it in excel from the csv file with formatting parameters I obtain really strange formatting for float values like 69.5000 or 5.2.0000000 or date times formatting too like this:

NORM    INTERO  13.01
NORM    P.S.    69.05.00
NORM    SCREMATO    5.02

Consider that I m using European format ("," as decimal as I use to import the original raw data from csv files).

Please help me: I developed a software (with GUI and so on) and I cant deliver it for that reason!

Thanks

3
  • What does the csv look like when opened with a text editor (e.g. in notepad) Commented Jan 8, 2013 at 15:36
  • like this: AD;P.S.;8.3 AD;SCREMATO;0.6 CRE;STD;1.2 FUN;INTERO;0.0 FUN;P.S.;2.0 FUN;SCREMATO;0.0 NORM;INTERO;13.1 NORM;P.S.;69.5 NORM;SCREMATO;5.2 Commented Jan 8, 2013 at 15:58
  • if I save it from notepad and I try to open it with excel I obtain the same problem: AD P.S. 8.03 AD SCREMATO 0.06 CRE STD 1.02 FUN INTERO 0.00 FUN P.S. 2.00 FUN SCREMATO 0.00 NORM INTERO 13.01 NORM P.S. 69.05.00 NORM SCREMATO 5.02 Commented Jan 8, 2013 at 16:02

1 Answer 1

6

You should use the to_excel DataFrame method:

# first convert Series to DataFrame
df_segmenti_t0 = DataFrame(segmenti_t0)

# save as excel spreadsheet
df_segmenti_t0.to_excel('prova.xls')
Sign up to request clarification or add additional context in comments.

14 Comments

I obtain this: In [3]: segmenti_t0.to_excel('prova.csv') --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) C:\Windows\system32\<ipython-input-3-2adcb4ade28e> in <module>() ----> 1 segmenti_t0.to_excel('prova.csv') AttributeError: 'Series' object has no attribute 'to_excel'
@ManuelZompetta I missed it was a series not a DataFrame, please see updated answer. to_excel is a DataFrame method, so you must convert the Series to a DataFrame (segmenti_t0 = pd.DataFrame(segmenti_t0)).
Now I obtain this: C:\Python27\lib\site-packages\pandas\io\parsers.pyc in __init__(self, path) 2108 self.fm_date = xlwt.easyxf(num_format_str='YYYY-MM-DD') 2109 else: -> 2110 from openpyxl.workbook import Workbook 2111 self.book = Workbook()#optimized_write=True) 2112
and... ImportError: No module named openpyxl.workbook maybe the library is not properly installed? I work on Enthought distribution 7.3 Thanks again!
@ManuelZompetta it looks like openpyxl is not in Enthought Free, you'll need to install it separately...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.