0
$\begingroup$

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)

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
$\endgroup$
  • $\begingroup$ Can you post the first few lines of the CSV file that is generated? $\endgroup$ – Valentin Calomme May 20 at 13:36
0
$\begingroup$

It doesn't seem like a Pandas issue. Actually, I think that it is just Excel not realizing that your CSV file has multiple columns.

This can easily be dealt with by using Excel's "Text to Columns" feature.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.