繁体   English   中英

找到特定的列值后如何将一个数据帧拆分为多个数据帧

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM