Python 如何在找到特定列值后将数据帧拆分为多个数据帧
我有一个包含两列“ExplB”和“remP”的数据框架。remP中的值只能为0或1。在列remP中满足值1之后,我尝试将数据帧拆分为多个数据帧。如何在Python中执行此操作 我怎样才能解决这个问题?Python 如何在找到特定列值后将数据帧拆分为多个数据帧,python,pandas,dataframe,split,Python,Pandas,Dataframe,Split,我有一个包含两列“ExplB”和“remP”的数据框架。remP中的值只能为0或1。在列remP中满足值1之后,我尝试将数据帧拆分为多个数据帧。如何在Python中执行此操作 我怎样才能解决这个问题? 您可以使用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
您可以使用
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]
欢迎来到SO,请提供示例输入,不允许图像输入。似乎需要什么样的预期输出?
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]