Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a csv like this:

col1,col2,col2_val,col3,col3_val
A,1,3,5,6
B,2,3,4,5

and i want to transfer this csv like this :

col1,col6,col7,col8
A,Col2,1,3
A,col3,5,6

there are col3 and col3_val so i want to keep col3 in col6 and values of col3 in col7 and col3_val's value in col8 in the same row where col3's value is stored.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
122 views
Welcome To Ask or Share your Answers For Others

1 Answer

I think what you're looking for is df.melt and df.groupby:

In [63]: df.rename(columns=lambda x: x.strip('_val')).melt('col1')
           .groupby(['col1', 'variable'], as_index=False)['value'].apply(lambda x: pd.Series(x.values))
           .add_prefix('value')
           .reset_index()
Out[63]: 
  col1 variable  value0  value1
0    A     col2       1       3
1    A     col3       5       6
2    B     col2       2       3
3    B     col3       4       5

Credit to John Galt for help with the second part.

If you wish to rename columns, assign the whole expression above to df_out and then do:

df_out.columns = ['col1', 'col6', 'col7', 'col8']

Saving this should be straightforward with df.to_csv.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...