2019-05-07
How to write a “key” column with seperately with each other column to csv in python
源自: stackoverflow
问题

In Python I want to break up one data-frame and write -each "Key","Column" pair to a CSV file. The rows also sometimes contain more than one value per column.

The data-frame looks as follows (the number of columns will not always be the same)

col1     col2     key    col3
a        b        c      d
a1       b1       c1     d1,d4
a2       b2,b4    c2     d2
a3       b3       c3     d3

What I need is 3 CSV files, looking as follows:

col.csv
key    col1
c      a
c1     a1
c2     a2
c3     a3

col2.csv
key    col2
c      b
c1     b1
c2     b2
c2     b4
c3     b3

col3.csv
key    col3
c      d
c1     d1
c1     d4
c2     d2
c3     d3 

What I've done is write out each column to CSV, with the column name as the CSV, I however cannot get the "key" column to the CSV's.

z = df[df_obj.columns]

for c in z.columns:
    df[c].to_csv(cwd + '/split/' + c + '.csv', index=False)

What I get is:

col1.csv
col1
a
a1
a2
a3

col2.csv
col2
b
b1
b2,b4
b3

col3.csv
col3
d
d1,d4
d2
d3 
答案
1

Would you mind trying the following. The idea is just to extract the keys column out as a separate series first, which you can then use when you iterate over the columns.

import os
import pandas as pd

>>>df
col1     col2     key    col3
a        b        c      d
a1       b1       c1     d1,d4
a2       b2,b4    c2     d2
a3       b3       c3     d3

# Create a key series
keys = [r for r in df.key]
# Exclude the keys column from the dataframe
df = df.drop('key', axis=1)

for c in df.columns:
    ser1 = df[c].str.split(',')
    data = pd.DataFrame([(keys[i], j) for i in range(len(keys)) for j in ser1[i]])
    data.to_csv(os.path.join(os.getcwd(),c,'.csv'), index=False)
How to write a “key” column with seperately with each other column to csv in python
查看原网页