[英]Filtering of data in numpy array
我有一個 CSV 文件,其中包含來自世界不同地區的漲潮幅度的數據,我編寫了一個代碼來過濾從該 CSV 文件讀取的數據,代碼如下所示:
import pandas as pd
import numpy as np
df1=pd.read_csv("Tide Prediction.csv")
df1.columns = df1.iloc[0] #To replace the header with the first row
df1 = df1[1:]
df2=df1.rename(columns={df1.columns[3]: "location"})
dict = {'UTC': 'time',
'degrees_east': 'longitude',
'degress_west': 'latitude'}
df2['degrees_north'] = df2['degrees_north'].astype(float, errors = 'raise')
df2['degrees_east'] = df2['degrees_east'].astype(float, errors = 'raise')
c=np.where(degrees_north>8.06694 & degrees_north < 37.10028, [True]*6885393, [False]*6885393)
您可以between
使用:
假設如下 dataframe
df = pd.DataFrame({'degrees_north': [8, 9, 37, 38]})
print(df)
# Output:
degrees_north
0 8
1 9
2 37
3 38
>>> df['degrees_north'].between(8.06694, 37.10028, inclusive='neither')
0 False
1 True
2 True
3 False
Name: degrees_north, dtype: bool
這里np.where
不是必須的,因為同樣的 output 如果只比較,只需要 add ()
條件:
c=(degrees_north>8.06694) & (degrees_north < 37.10028)
c=np.where((degrees_north>8.06694) & (degrees_north < 37.10028), True, False)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.