Python 将行值分隔为新列

Python 将行值分隔为新列,python,pandas,dataframe,jupyter-notebook,Python,Pandas,Dataframe,Jupyter Notebook,我有这种数据帧输出,列在最上面一行: Date, Team1, Team2, Map, Event 17/3/17, Misfits 16, Cloud9 4, overpass, Pro League 17/3/17, TyLoo 16, Born Of Fire 4, cache, Pro League 17/3/17, Liquid 8, Renegades 16, cbble, Proleague 17/3/17, Earnix 16, Blight 7, overpass, Prole

我有这种数据帧输出,列在最上面一行:

Date, Team1, Team2, Map, Event
17/3/17, Misfits 16, Cloud9 4, overpass, Pro League
17/3/17, TyLoo 16, Born Of Fire 4, cache, Pro League
17/3/17, Liquid 8, Renegades 16, cbble, Proleague
17/3/17, Earnix 16, Blight 7, overpass, Proleague
17/3/17, Selfless 12, Rush 16,, inferno, Proleague
我的目标是获得这种输出:

Date, Team1, Team2, Team1 Score, Team2 Score, Map, Event
17/3/17, Misfits, Cloud9, 16, 4,overpass, Pro League
17/3/17, TyLoo, Born Of Fire, 16, 4, cache, Pro League
17/3/17, Liquid, Renegades, 8, 16, cbble, Proleague
17/3/17, Earnix, Blight, 16, 7, overpass, Proleague
17/3/17, Selfless, Rush, 12, 16, inferno, Proleague
如何将“Team1”和“Team2”列中的分数值分离为一个全新的列,命名为“Team1分数”和“Team2分数”?

您可以使用:

另一个解决方案:

orig_cols = df.columns.tolist()
df1 = df.filter(like='Team')
new_cols = []
for col in df1:
    df[[col, col + ' Score']] = df1[col].str.rsplit(n=1, expand=True)
    new_cols.append(col + ' Score')
print (df)
      Date     Team1         Team2       Map       Event Team1 Score  \
0  17/3/17   Misfits        Cloud9  overpass  Pro League          16   
1  17/3/17     TyLoo  Born Of Fire     cache  Pro League          16   
2  17/3/17    Liquid     Renegades     cbble   Proleague           8   
3  17/3/17    Earnix        Blight  overpass   Proleague          16   
4  17/3/17  Selfless          Rush   inferno   Proleague          12   

  Team2 Score  
0           4  
1           4  
2          16  
3           7  
4          16 

#get only Team columns
df1 = df.filter(like='Team')
cols = df1.columns.tolist()
df1 = pd.concat([df1[x].str.rsplit(n=1, expand=True) for x in df1], axis=1, keys=df1.columns)
df1 = df1.sort_index(level=1, axis=1).rename(columns={0: '', 1: ' Score'})
cols2 = df1.columns.map(''.join).tolist()
df1.columns = cols2
print (df1)
      Team1         Team2 Team1 Score Team2 Score
0   Misfits        Cloud9          16           4
1     TyLoo  Born Of Fire          16           4
2    Liquid     Renegades           8          16
3    Earnix        Blight          16           7
4  Selfless          Rush          12          16

#add to original df
df2 = pd.concat([df.drop(cols, axis=1), df1], axis=1)
#change order of columns
df2 = df2.reindex_axis(df.columns[:1].tolist() + cols2 + df.columns[-2:].tolist(), axis=1)
print (df2)
      Date     Team1         Team2 Team1 Score Team2 Score       Map  \
0  17/3/17   Misfits        Cloud9          16           4  overpass   
1  17/3/17     TyLoo  Born Of Fire          16           4     cache   
2  17/3/17    Liquid     Renegades           8          16     cbble   
3  17/3/17    Earnix        Blight          16           7  overpass   
4  17/3/17  Selfless          Rush          12          16   inferno   

        Event  
0  Pro League  
1  Pro League  
2   Proleague  
3   Proleague  
4   Proleague
orig_cols = df.columns.tolist()
df1 = df.filter(like='Team')
new_cols = []
for col in df1:
    df[[col, col + ' Score']] = df1[col].str.rsplit(n=1, expand=True)
    new_cols.append(col + ' Score')
print (df)
      Date     Team1         Team2       Map       Event Team1 Score  \
0  17/3/17   Misfits        Cloud9  overpass  Pro League          16   
1  17/3/17     TyLoo  Born Of Fire     cache  Pro League          16   
2  17/3/17    Liquid     Renegades     cbble   Proleague           8   
3  17/3/17    Earnix        Blight  overpass   Proleague          16   
4  17/3/17  Selfless          Rush   inferno   Proleague          12   

  Team2 Score  
0           4  
1           4  
2          16  
3           7  
4          16 
#get position of last column in df1
splitted = df.columns.get_loc(df1.columns[-1]) + 1
#change order
df2 = df.reindex_axis(orig_cols[:splitted] + new_cols + orig_cols[splitted:], axis=1)
print (df2)
      Date     Team1         Team2 Team1 Score Team2 Score       Map  \
0  17/3/17   Misfits        Cloud9          16           4  overpass   
1  17/3/17     TyLoo  Born Of Fire          16           4     cache   
2  17/3/17    Liquid     Renegades           8          16     cbble   
3  17/3/17    Earnix        Blight          16           7  overpass   
4  17/3/17  Selfless          Rush          12          16   inferno   

        Event  
0  Pro League  
1  Pro League  
2   Proleague  
3   Proleague  
4   Proleague  
s = df.set_index(['Date', 'Map', 'Event']).stack()
d = s.str.extract(
    '(.*)\s+(\S+)', expand=True
).rename(columns={0: '', 1: ' Score'}).unstack()
d.columns = d.columns.map('{0[1]}{0[0]}'.format)
d.reset_index()