[英]How to split a dataframe in multiple dataframes after a specific column value is found
我有兩列“ ExplB”和“ remP”的數據框。 remP中的值只能為0或1。在列remP中滿足值1之后,我試圖將數據幀拆分為多個數據幀。 如何在Python中執行此操作?
我該如何解決? 在此處輸入圖片說明
data = {'ExplB':[0,0,0,0.2,0.2,0.15,0,0,0,0,0,0,0,0,0],'remP':[0,0,0,1,0,0,0,0,1,0,0,0,1,0,0]}
df = pd.DataFrame(data, columns = ['ExplB', 'remP'])
你可以使用np.split
# find the index where df['remP']==1
idx = df[df['remP']==1].index
# split your df on that index
dfs = np.split(df, idx)
[ ExplB remP
0 0.0 0
1 0.0 0
2 0.0 0, ExplB remP
3 0.20 1
4 0.20 0
5 0.15 0
6 0.00 0
7 0.00 0, ExplB remP
8 0.0 1
9 0.0 0
10 0.0 0
11 0.0 0, ExplB remP
12 0.0 1
13 0.0 0
14 0.0 0]
或者如果您想在該索引之后拆分df,請執行idx + 1
idx = df[df['remP']==1].index
dfs = np.split(df, idx+1)
[ ExplB remP
0 0.0 0
1 0.0 0
2 0.0 0
3 0.2 1, ExplB remP
4 0.20 0
5 0.15 0
6 0.00 0
7 0.00 0
8 0.00 1, ExplB remP
9 0.0 0
10 0.0 0
11 0.0 0
12 0.0 1, ExplB remP
13 0.0 0
14 0.0 0]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.