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

2024-03-28 18:52:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个由两列组成的数据框“ExplB”和“remP”。remP中的值只能是0或1。我试图在remP列中满足值1后将dataframe拆分为多个dataframe。如何在Python中执行此操作?在

我怎么解决这个问题? enter image description here

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'])

Tags: columns数据imagedataframedfdataheredescription
1条回答
网友
1楼 · 发布于 2024-03-28 18:52:39

您可以使用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

^{pr2}$

相关问题 更多 >