我有一個看起來像這樣的資料框
table = {'0': {6: 'Becks', 7: '307NRR', 8: '321NRR', 9: '342NRR', 10: 'Campbell', 11: '329NRR', 12: '347NRR', 13: 'Crows', 14: 'C3001R'}, '1': {6: nan, 7: 'R', 8: 'R', 9: 'R', 10: nan, 11: 'R', 12: 'R', 13: nan, 14: 'R'}, '2': {6: nan, 7: 'CM,SG', 8: 'CM,SG', 9: 'CM,SG', 10: nan, 11: 'None', 12: 'None', 13: nan, 14: 'None'}, '3': {6: nan, 7: 3.0, 8: 3.2, 9: 3.4, 10: nan, 11: 3.2, 12: 3.4, 13: nan, 14: 3.0}}
0 1 2 3
6 Becks NaN NaN NaN
7 307NRR R CM,SG 3.0
8 321NRR R CM,SG 3.2
9 342NRR R CM,SG 3.4
10 Campbell NaN NaN NaN
11 329NRR R None 3.2
12 347NRR R None 3.4
13 Crows NaN NaN NaN
14 C3001R R None 3.0
我想使用帶有名稱的行Becks, Campbell, Crows作為單獨的列來命名它們下面的條目。所以結果看起來像:
Becks 307NRR R CM,SG 3.0
Becks 321NRR R CM,SG 3.2
Becks 342NRR R CM,SG 3.4
Campbell 329NRR R None 3.2
Campbell 347NRR R None 3.4
Crows C3001R R None 3.0
處理熊貓/基礎模塊的好方法是什么?
uj5u.com熱心網友回復:
嘗試:
# your header rows
headers = df.iloc[:, 1:].isna().all(1)
# propagate the headers to the rows below it
df['name'] = df.groupby(headers.cumsum())['0'].transform('first')
# also
# df['name'] = df.iloc[:,0].where(headers).ffill()
# drop the header rows
df = df[~headers]
輸出:
0 1 2 3 name
7 307NRR R CM,SG 3.0 Becks
8 321NRR R CM,SG 3.2 Becks
9 342NRR R CM,SG 3.4 Becks
11 329NRR R None 3.2 Campbell
12 347NRR R None 3.4 Campbell
14 C3001R R None 3.0 Crows
請注意,如果您真的關心列順序,df['name'] = ...您可以使用它insert來匹配您的預期輸出:
df.insert(0, 'name', df.iloc[:,0].where(headers).ffill())
你會得到:
name 0 1 2 3
7 Becks 307NRR R CM,SG 3.0
8 Becks 321NRR R CM,SG 3.2
9 Becks 342NRR R CM,SG 3.4
11 Campbell 329NRR R None 3.2
12 Campbell 347NRR R None 3.4
14 Crows C3001R R None 3.0
uj5u.com熱心網友回復:
咱們試試吧
df['new_co'] =df['0'].shift()
df=df.assign(new_co=np.where(df['new_co'].str.contains('RR'),np.nan,df['new_co'])).dropna(thresh=3).ffill()
0 1 2 3 new_co
7 307NRR R CM,SG 3.0 Becks
8 321NRR R CM,SG 3.2 Becks
9 342NRR R CM,SG 3.4 Becks
11 329NRR R None 3.2 Campbell
12 347NRR R None 3.4 Campbell
14 C3001R R None 3.0 Crows
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/355475.html
上一篇:布爾資料幀的支持矩陣
