创建条件子数据框架

huangapple go评论55阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年7月14日 08:05:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定