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

How to Group Consecutive Values in a Pandas DataFrame Column?

Barbara Streisand
Release: 2024-11-27 20:37:15
Original
517 people have browsed it

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
Copy after login

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

[1, 1]
[-1]
[1]
[-1, -1]
Copy after login

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())
Copy after login

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]
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template