英文:
Creating conditional sub dataframe
问题
我有以下的数据框。如何基于列中的连续条件创建子数据框?例如,在下面的数据框中,我想根据列B中连续的"1s"创建单独的数据框。所以,在这个示例中,期望的输出将是三个单独的数据框,分别包含行1和2,行4,以及行6到9。谢谢。
level_1 = ['A', 'B']
data = [['a1', 1], ['a2', 1], ['a3', 0], ['a4', 0], ['a1', 1], ['a5', 0], ['a6', 1], ['a7', 1], ['a8', 1], ['a9', 1]]
df = pd.DataFrame(data, columns=level_1)
英文:
I have the below dataframe. How do I create sub dataframes based on a continous condition in a column? For example, in the below dataframe, I want to create a separate dataframe for each occurrence of continuous "1s" in column B. So, in this example, the desired output would be three separate dataframes for rows 1 & 2, 4, and 6-9. Thank you.
level_1 = ['A', 'B']
data = [['a1', 1], ['a2', 1],['a3', 0], ['a4', 0],['a1', 1], ['a5', 0],['a6', 1], ['a7', 1],['a8', 1], ['a9', 1]]
df = pd.DataFrame(data, columns=level_1)
答案1
得分: 1
这是一种将它们分类到不同组中的方法
df['cat'] = (df['B'].diff() != 0).cumsum()
# 过滤掉您不想要的行/组
df = df[df['B'] == 1]
输出:
A B cat
0 a1 1 1
1 a2 1 1
4 a1 1 3
6 a6 1 5
7 a7 1 5
8 a8 1 5
9 a9 1 5
英文:
here is one way to categorize them into separate groups
df['cat'] = (df['B'].diff() != 0).cumsum()
# filter out the rows/groups you don't want
df = df[df['B'] == 1]
output :
A B cat
0 a1 1 1
1 a2 1 1
4 a1 1 3
6 a6 1 5
7 a7 1 5
8 a8 1 5
9 a9 1 5
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论