Can pandas split/merge columns based on patterns in their name?

帅比萌擦擦* 提交于 2020-01-04 00:14:29

问题


Can pandas split and/or merge columns, based on patterns in the column name? Here's a DataFrame:

    meas1_left  meas1_right  meas2_left  meas2_right
0            1            2           3            4
1            6            7           8            9

I'd like to turn the above data and this (I don't really care how the new frame is indexed):

    meas1  meas2  side
0       1      3  left
1       2      4  right
2       6      8  left
3       7      9  right

回答1:


You can first create Multiindex from columns by split:

df.columns = df.columns.str.split('_', expand=True)
print (df)
  meas1       meas2      
   left right  left right
0     1     2     3     4
1     6     7     8     9

Then stack it:

print (df.stack().reset_index(level=0, drop=True).reset_index())
   index  meas1  meas2
0   left      1      3
1  right      2      4
2   left      6      8
3  right      7      9

And if need rename column index and change order of columns:

print (df.stack()
         .reset_index(level=0, drop=True)
         .reset_index()
         .rename(columns={'index':'side'})[['meas1','meas2','side']])

   meas1  meas2   side
0      1      3   left
1      2      4  right
2      6      8   left
3      7      9  right

EDIT: str methods with index are implemented from 0.16.1, if use older version try:

a = df.columns.to_series().str.split('_').apply(pd.Series)
tuples = list(zip(a.iloc[:,0], a.iloc[:,1]))
print (tuples)
[('meas1', 'left'), ('meas1', 'right'), ('meas2', 'left'), ('meas2', 'right')]

df.columns = pd.MultiIndex.from_tuples(tuples)
print (df)
  meas1       meas2      
   left right  left right
0     1     2     3     4
1     6     7     8     9


来源:https://stackoverflow.com/questions/39390615/can-pandas-split-merge-columns-based-on-patterns-in-their-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!