Home  >  Article  >  Backend Development  >  How to Group Consecutive Values in a Pandas DataFrame Column?

How to Group Consecutive Values in a Pandas DataFrame Column?

Barbara Streisand
Barbara StreisandOriginal
2024-11-27 20:37:15459browse

How to Group Consecutive Values in a Pandas DataFrame Column?

Grouping Consecutive Values in a Pandas DataFrame

This question seeks a solution to group consecutive values in a DataFrame column. Consider the following DataFrame with the column 'a':

   a
0  1
1  1
2 -1
3  1
4 -1
5 -1

The goal is to group these values into sublists representing consecutive sequences, as shown below:

[1, 1]
[-1]
[1]
[-1, -1]

Solution Using Custom Series

To achieve this, we can leverage custom Series to identify consecutive value breaks. The following code demonstrates this approach:

df = pd.DataFrame({'a': [1, 1, -1, 1, -1, -1]})
print(df)

# Create a series that identifies consecutive value breaks
breaks = df['a'].ne(df['a'].shift()).cumsum()
print(breaks)

# Group the DataFrame by the breaks series
for i, g in df.groupby(breaks):
    print(i)
    print(g)
    print(g.a.tolist())

The output shows the consecutive value groupings as required:

1
   a
0  1
1  1
[1, 1]
2
   a
2 -1
[-1]
3
   a
3  1
[1]
4
   a
4 -1
5 -1
[-1, -1]

The above is the detailed content of How to Group Consecutive Values in a Pandas DataFrame Column?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn