Home > Mobile >  Is there a method that could take an initial large dataframe and process this into smaller segments
Is there a method that could take an initial large dataframe and process this into smaller segments

Time:12-06

I would like to take an initial dataframe and split this into multiple smaller dataframes based on angle size.

I've tried using for loops and the pandas drop functionality but this has not been successful so far.

I would like to split the dataframes so that it contains all angles between -60 to -55 and then -55 to 50 and so on in 5 degree increments.This is my ideal expected outcome.

CodePudding user response:

Supposing you have and your initial dataframe names df with a column names angle , you can split it into 2 dataframes on mentioned conditions like this:

df1 = df[df["angle"].between(-60, -55)]
df2 = df[df["angle"].between(-55, -50)]

CodePudding user response:

pd.cut should be useful here. It splits your column into bins.

import pandas as pd

df = pd.DataFrame({'angle': range(-100, 0)})

# 1. add new categorical column
angle_min, angle_max = df.angle.min(), df.angle.max()
step = 5
ir = pd.interval_range(start=angle_min, end=angle_max, freq=step, closed='left')
df['angle_cat'] = pd.cut(df['angle'], ir)

# 2. group by categorical column
for ind, sub_df in df.groupby('angle_cat'):
    print(sub_df)
>>>    angle    angle_cat
>>> 0   -100  [-100, -95)
>>> 1    -99  [-100, -95)
>>> 2    -98  [-100, -95)
>>> 3    -97  [-100, -95)
>>> 4    -96  [-100, -95)
>>>    angle   angle_cat
>>> 5    -95  [-95, -90)
>>> 6    -94  [-95, -90)
>>> 7    -93  [-95, -90)
>>> 8    -92  [-95, -90)
>>> 9    -91  [-95, -90)
>>> ...
  • Related