1

I am exporting some info to csv files. I am using the following code:

   for i, g in df.sort_values("date").groupby("country", sort=False):
    if Curency == 'USD':
        g.to_csv('{}_USD.csv'.format(i))
    else:
        g.to_csv('{}_NK.csv'.format(i))

When I open the csv files I get the csv files; but in just one column.

enter image description here

I would like to get the information, contained in each csv file, separated by columns (using comma to separate the information) like this.

enter image description here

I followed the documentation stated here but I couldn´t get my desire output.

Any help would be appreciated. Thanks

Newbie
  • 451
  • 1
  • 3
  • 14
  • 1
    you want to pass `decimal = ','` to `to_csv` – EdChum Jan 18 '18 at 15:26
  • Actually I don't understand what you want, can you post raw data, your code and what the desired output should look like. – EdChum Jan 18 '18 at 15:36
  • What does your file look like in a text editor? To me it looks like you're getting a csv file and are just having trouble opening it with your spreadsheet. Looking at the file with a text editor will show what's really there. – Steven Rumbalski Jan 18 '18 at 16:05
  • That´s exactly what it is happeing. The problem is when I open the cvs file as an Excel one. Is there any way to change the information format to fix the issue in the spreasdsheet? – Newbie Jan 18 '18 at 16:19
  • Okay. When you do the File Open dialog in Excel and choose your csv file it gives you the Text Import Wizard. The result you are showing is if you click Finish without going through the wizard. First click Next, then change the Delimiters checkbox from Tab to Comma. At this point you can get away with clicking Finish. But if you want to control the data type of each column you would click Next and adjust each column accordingly. – Steven Rumbalski Jan 18 '18 at 16:26
  • I'm voting to close this question as off-topic because this is not a programming question. – Steven Rumbalski Jan 18 '18 at 16:34
  • ok, I noted this is an issue about how Excel read the file. I thought this can be handle inside the code but if not (specially if a thousands of csv files), I agree with you. – Newbie Jan 18 '18 at 16:41
  • 1
    If you want something that is native to Excel then use `to_excel()`. This is not a pandas limitation, it's just how Excel works. – Steven Rumbalski Jan 18 '18 at 16:47

1 Answers1

-1

You should set the flag index=False when saving csv.

g.to_csv(filename, index=False)

Then you can import the csv in Excel instead of opening it. https://support.office.com/en-gb/article/Import-or-export-text-txt-or-csv-files-5250ac4c-663c-47ce-937b-339e391393ba

  • Not having the index can be a nice thing, but does not address his question. If the OP double-clicks a csv file in Windows if it's associated with Excel it will be parsed into the correct rows and columns (although the data types may be mishandled). If the OP opens with the File Open dialog he gets pushed into the Text Import Wizard. If he clicks Finish without selecting any options his csv is treated as a tab delimited file. I think that's the root problem. – Steven Rumbalski Jan 18 '18 at 16:32