我有一个DataFrame
看起来像这样:
import numpy as np
import pandas as pd
df=pd.DataFrame([['d',5,6],['a',6,6],['index',5,8],['b',3,1],['b',5,6],['index',6,7],
['e',2,3],['c',5,6],['index',5,8]],columns=['A','B','C'])
我想选择index
之间的所有行并创建许多数据框
我想以:p
dataframe1:
A B C
1 a 6 6
2 index 5 8
3 3 b 3
数据框2
A B C
4 b 5 6
5 index 6 7
6 c 2 3
datframe3:
A B C
7 c 5 6
8 index 5 8
9 4 3 1
dataframe4:
A B C
11 5 2 3
12 index 4 2
13 1 2 5
答案 0 :(得分:0)
index_list = df.index[df['A'] == 'index'].tolist() # create a list of the index where df['A']=='index'
new_df = [] # empty list for dataframes
for i in index_list: # for loop
try:
new_df.append(df.iloc[i-1:i+2])
except:
pass
这将创建一个数据帧列表,您可以通过new_df[0]
new_df[1]
对其进行调用,也可以使用循环将其打印出来:
for i in range(len(new_df)):
print(f'{new_df[i]}\n')
A B C
1 a 6 6
2 index 5 8
3 b 3 1
A B C
4 b 5 6
5 index 6 7
6 e 2 3
A B C
7 c 5 6
8 index 5 8