0

When I convert my pandas dataframe into a csv it all puts it in one column, I would like it to be in multiple columns.

this my code

df = pd.concat([data_2020, data_2019Q4, data_2019Q3, data_2019Q2, data_2019Q1, data_2018QH1, data_2017H1,
                data_2016H1, data_2015H1])

split_data = df["Date,Country,City,Specie,count,min,max,median,variance"].str.split(",")
data = split_data.to_list()
names = ['Date', 'Country', 'City', 'Specie', 'count', 'min', 'max', 'median', 'variance']
new_df = pd.DataFrame(data, columns=names)

new_df.to_csv('concat_data1.csv', sep=",",header=None,columns=names)

Edit :

Sample of my dataframe

               Date Country      City Specie count   min    max median variance
0        2020-02-02      RS  Novi Sad   pm10    48   4.0   42.0   14.0   742.70
1        2020-02-05      RS  Novi Sad   pm10    48   5.0   21.0    9.0   115.46
2        2020-02-24      RS  Novi Sad   pm10    48  10.0   99.0   27.0  3611.91
3        2020-03-02      RS  Novi Sad   pm10    41   3.0   68.0   14.0  1515.22
4        2020-05-09      RS  Novi Sad   pm10    36   9.0  102.0   15.0  5511.02
3
  • add your top 5 rows sample Commented May 15, 2020 at 17:35
  • just added it! @pyd Commented May 18, 2020 at 14:44
  • i am confused. CSV is just a text file where "columns" are denotes by commas. However, the CSV file itself is just strings on a line. If you choose to open that CSV in Excel, the you can tell excel how to read those commas as delimiters or as all text in a cell per line. Commented May 20, 2020 at 15:37

2 Answers 2

1

Worked by replacing the sep "," by ";"

Sign up to request clarification or add additional context in comments.

Comments

0

Could you use sth like new_df.to_excel('concat_data.xlsx')? This stores your df column-wise. However, this stores it as an Excel-file, not csv, so this solution only works for you if you are further processing/opening the data in Excel.

5 Comments

there are too many rows for it to fit in an excel file that's why I used to_csv
hm..ok. do you just need to save the dataframe somewhere for later use (in any format) or does it specifically have to be .csv?
it has to be .csv or .excel yes
Maybe this helps: link
thank you <3 it did actually work with replacing sep="," by sep=";". I don't why but cool

Your Answer

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