我有一個這樣的資料框:
dummy_df = pd.DataFrame([{'Name': 'First', 'Value': 1}, {'Name': 'Start of', 'Value': None}, {'Name': 'cut off', 'Value': None}, {'Name': 'Last', 'Value': 10}, {'Name': 'First of', 'Value': None}, {'Name': 'three lines', 'Value': None}, {'Name': 'cut off', 'Value': None}, {'Name': 'Actually last', 'Value': 100}])
Name Value
0 First 1.0
1 Start of NaN
2 cut off NaN
3 Last 10.0
4 First of NaN
5 three lines NaN
6 cut off NaN
7 Actually last 100.0
我行的任意數字,其中Value列NaN,我想結合連續None這樣的行Name被連接起來以產生一個最終的結果,例如:
Name Value
0 First 1.0
1 Start of cut off NaN
2 Last 10.0
3 First of three lines cut off NaN
4 Actually last 100.0
我已經使用了一些其他的答案嘗試使用df.shift,并cumsum無濟于事。
uj5u.com熱心網友回復:
嘗試cumsum在notna該塊,然后GROUPBY:
s = dummy_df['Value'].notna()
dummy_df.groupby([s,s.cumsum()], as_index=False, sort=False).agg({
'Name': ' '.join,
'Value': 'first'
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/313001.html
