简体   繁体   English

在 Pandas 中创建一个新列

[英]Creating a new column in Pandas

Thank you in advance for taking the time to help me!预先感谢您抽出时间帮助我! (Code provided below) ( Data Here ) (下面提供了代码)( 数据在这里

I am trying to average the first 3 columns and insert it as a new column labeled 'Topsoil'.我正在尝试对前 3 列求平均值并将其插入为标有“表土”的新列。 What is the best way to go about doing that?这样做的最佳方法是什么?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
raw_data = pd.read_csv('all-deep-soil-temperatures.csv', index_col=1, parse_dates=True)
df_all_stations = raw_data.copy()
df_selected_station.fillna(method = 'ffill', inplace=True);
df_selected_station_D=df_selected_station.resample(rule='D').mean()
df_selected_station_D['Day'] = df_selected_station_D.index.dayofyear
mean=df_selected_station_D.groupby(by='Day').mean()
mean['Day']=mean.index
#mean.head()

在此处输入图片说明

尝试这个 :

mean['avg3col']=mean[['5 cm', '10 cm','15 cm']].mean(axis=1)
df['new column'] = (df['col1'] + df['col2'] + df['col3'])/3

You could use the apply method in the following way:您可以通过以下方式使用apply方法:

mean['Topsoil'] = mean.apply(lambda row: np.mean(row[0:3]), axis=1)

You can read about the apply method in the following link: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html您可以在以下链接中了解apply方法: https : //pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html

The logic is that you perform the same task along a specific axis multiple times.逻辑是您沿着特定轴多次执行相同的任务。

Note: It is not wise to call data-structures in names of functions, in your case it might be better be mean_df rather the mean注意:在函数名称中调用数据结构是不明智的,在您的情况下,最好是mean_df而不是mean

使用DataFrame.iloc按位置选择 - 前 3 列mean

mean['Topsoil'] = mean.iloc[:, :3].mean(axis=1)

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

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