Search code examples
pythonpandasdataframecsvexport-to-csv

How to save a column of elements as a row, in a csv file?


How can I save a column of elements, as a row/list, in a CSV file?

Here following the relevant part of my code...

import pandas as pd

df=pd.DataFrame(tbl,columns=['type','skill','job1','job2','m1','m2'])
df2=df['skill']

print(type(df2))
print(df2)

...and its corresponding result...

<class 'pandas.core.series.Series'>

1                statistics
26         highly motivated
32                     team
...
93                knowledge
94                  curious
95                      can
96                    lives
98             early talent
99           talent program
100         computer skills
101     process development
102             soft skills
103           early program
Name:  skill, dtype: object

My desired output would be a CSV file with all the words listed in a row, and delimited by a comma, i.e.

statistics, highly motivated, team, ..., knowledge, curious, can, lives, early talent, ...

Solution

  • You can do this... for any column to get data as required;

    col_to_str = ", ".join(list(df['Column_Name']))
    # Export as CSV
    import pandas as pd
    df1 = pd.DataFrame({"Column_Name":[col_to_str]})
    df1.to_csv("export.csv",index=False)
    

    Hope this Helps...