简体   繁体   English

如何按纬度在 90 到 -90 范围内拆分 dataframe

[英]How to split dataframe by latitude in ranges from 90 to -90

I have dataframe that I need to split based on latitude.我有 dataframe 需要根据纬度进行拆分。

The example of dataframe columns: dataframe 列的示例:

df.glat = [-20, -30, -40, -50, -60, -70, -80, -90, -80, -70 - 60, -50, -40, -30, -20, -10, 10, 20, 30]

I was trying to create a column status based on behavior of latitude values:我试图根据纬度值的行为创建列状态:

df. status = ['decline', 'decline', 'decline', 'decline', 'decline', 'decline', 'decline', 'decline', 'increase', 'increase', 'increase', 'increase', 'increase', 'increase', 'increase', 'increase', 'increase', 'increase', 'increase']

I need to group it into groups from 90 to -90.我需要将其分组为从 90 到 -90 的组。 In original my dataframe change in this ranges several time.在原来我的 dataframe 在这个范围内改变了几次。

How can split dataframes into such groups:如何将数据帧分成这样的组:

1 : [-20, -30, -40, -50, -60, -70, -80, -90]
2: [ -80, -70 - 60, -50, -40, -30, -20, -10, 10, 20, 30]

I have made a graphic of my ordinal data how the latitude changes:我已经制作了我的序数数据的图表,纬度如何变化: 在此处输入图像描述 So I need to split each range from [-90 to 90] as separate dataframe.所以我需要将每个范围从 [-90 到 90] 拆分为单独的 dataframe。 Like here from the graphic for example should be 16 splitted dataframes.像这里的图形一样,例如应该是 16 个拆分的数据帧。

I will be grateful for any suggestion!我将不胜感激任何建议!

df = pd.DataFrame({'glat': [-20, -30, -40, 10, 20, 30, -40, -50, -60]})

df['is_increasing'] = df['glat'].diff().bfill().gt(0)
df['group'] = df['is_increasing'].diff().bfill().cumsum()

df.groupby('group')['glat'].apply(list)

Output: Output:

group
0    [-20, -30, -40]
1       [10, 20, 30]
2    [-40, -50, -60]
Name: glat, dtype: object

PS df will get a group column with group ID, so you can easily split it into separate dataframes later (for example, with dataframes = [df for _, df in df.groupby('group')] ): PS df将获得一个带有组 ID 的group列,因此您可以稍后轻松地将其拆分为单独的数据帧(例如,使用dataframes = [df for _, df in df.groupby('group')] ):

   glat  is_increasing  group
0   -20          False      0
1   -30          False      0
2   -40          False      0
3    10           True      1
4    20           True      1
5    30           True      1
6   -40          False      2
7   -50          False      2
8   -60          False      2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM