簡體   English   中英

Pandas 數據幀最小/最大范圍

[英]Pandas Dataframe Min/Max Range

預先感謝您的幫助! (下面的代碼)/數據在這里: 鏈接

我正在嘗試向我的數據框中添加另外兩列,它們代表 Topsoil 列的數據范圍,就像 mean['maxx20']=maxx['20 cm'] 和 mean['minn20']=minn['20 cm'] 為 20 cm 列做。

我嘗試通過添加以下內容來做到這一點:

mean['topsoilMax']=maxx['Topsoil']
mean['topsoilMin']=minn['Topsoil']

這並沒有像我希望的那樣添加額外的列,而是導致KeyError: 'Topsoil'即使 Topsoil 已經是數據框中的一列,就像我添加范圍時的 20 cm 一樣。

為什么我會收到此錯誤以及添加這些列的正確方法是什么?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')

#Importing data, creating a copy, and assigning it to a variable
raw_data = pd.read_csv('all-deep-soil-temperatures.csv', index_col=1, parse_dates=True)
df_all_stations = raw_data.copy()

#Setting the program to iterate based off of the station of the users choice
selected_soil_station = 'Minot'
df_selected_station = df_all_stations[df_all_stations['Station'] == selected_soil_station]
df_selected_station.fillna(method = 'ffill', inplace=True);

# Indexes the data by day and creates a column that keeps track of the day
df_selected_station_D=df_selected_station.resample(rule='D').mean()
df_selected_station_D['Day'] = df_selected_station_D.index.dayofyear


#Assigning variable so that mean represents df_selected_station_D but indexed by day
mean=df_selected_station_D.groupby(by='Day').mean()
mean['Day']=mean.index

#This inserts a new column named 'Topsoil' at the end that represents the average between 5 cm, 10 cm, and 20 cm
mean['Topsoil']=mean[['5 cm', '10 cm','20 cm']].mean(axis=1)


#Creating the range in which the line graph will fill in 
maxx=df_selected_station_D.groupby(by='Day').max()
minn=df_selected_station_D.groupby(by='Day').min()

mean['maxx20']=maxx['20 cm']
mean['minn20']=minn['20 cm']

在此處輸入圖片說明 在此處輸入圖片說明

在此處輸入圖像描述如果我了解您的問題,那么我的解決方法是,

表土 = [-2.971686,-2.599278,-2.264897,-2.083117,-1.946969]

max_number = max(topsoil) min_number = min(topsoil) print(max_number) #這里得到表土列表的最大個數 print(min_number) #這里得到表土列表的最小個數 print(max_number - min_number) #這里你得到表土列表的最大 - 最小數量

這里的解決方案

可能需要將“Topsoil”列添加到 maxx 和 minn 數據幀:

maxx['Topsoil']=maxx[['5 cm', '10 cm','20 cm']].max(axis=1)
minn['Topsoil']=minn[['5 cm', '10 cm','20 cm']].min(axis=1)

任務完成后:

mean['topsoilMax']=maxx['Topsoil']
mean['topsoilMin']=minn['Topsoil']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM