【问题标题】:creating a new numpy array from a datframe based on quadrants从基于象限的数据框创建一个新的 numpy 数组
【发布时间】:2021-11-25 03:54:58
【问题描述】:

我想循环通过 pandas 数据框列或 numpy 数组,以使用条件格式将新列或 numpy 数组添加到原始数据框,以辅助航向角的象限。 COG 列包含我想知道角度象限的船舶路线

类似这样的:

['cog']  ---   ['quad_cog']
53.250000  --- 1
111.450001 --- 2
231.675003 --- 3
336.000000 --- 4

条件格式:

  0 > cog >  90 --- 1 
 90 > cog > 180 --- 2
180 > cog > 270 --- 3
270 > cog > 360 --- 4
import numpy as np
import pandas as pd

url = 'https://github.com/LukeCaptainAI/norm_quad/blob/main/aisdecoded.csv?raw=true' # forked from github
df = pd.read_csv(url, index_col=0)
df.head()

# Numpy array of interst:
cog = np.array(df['cog'])

如何循环遍历数据框 df['cog'] 或 numpy 数组,根据它们各自的象限对角度进行编号?

【问题讨论】:

    标签: python pandas numpy conditional-statements


    【解决方案1】:

    使用 np.select()

    np.select([(df.cog>0)&(df.cog<90),
              (df.cog>90)&(df.cog<180),
               (df.cog>180)&(df.cog<270),
              (df.cog>270)&(df.cog<360)],
             [1,2,3,4])
    

    【讨论】:

      猜你喜欢
      • 2017-12-30
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 2018-05-20
      相关资源
      最近更新 更多