简体   繁体   English

如何在 pandas dataframe 中创建新列,该计算发生在除 go 之外的每一行之外

[英]How to create new column in pandas dataframe of a calculation that happens to every other row except the one where the calculation will go into

For example let's say I got dataframe df with series A.1 and A.2 like so:例如,假设我得到了 A.1 和 A.2 系列的 dataframe df,如下所示:

A.1    A.2
2      8
3      2
5      1

And I want to calculate let's say the difference of the means of all other rows like so:我想计算让我们说所有其他行的平均值的差异,如下所示:

A.1    A.2    B
2      8      (3+5)/2 - (2+1)/2
3      2      (2+5)/2-(8+1)/2
5      1      (2+3)/2-(8+2)/2

My code looks like this and doesn't work, how should I write it correctly?我的代码看起来像这样并且不起作用,我应该如何正确编写它?

df['B'] = mean(df['A.1'].drop(df['B'].index)))-mean(df['A.2'].drop(df['B'].index)))

I MUST totally avoid loops and do it in a panda-ish way as I'm working with huge datasets.在处理庞大的数据集时,我必须完全避免循环并以熊猫式的方式进行。

Try:尝试:

df.apply(lambda r : df.loc[df.index!=r.name,'A.1'].mean() - df.loc[df.index!=r.name,'A.2'].mean(), axis = 1)

result set is:结果集是:

0    2.5
1   -1.0
2   -2.5
dtype: float64

Note that r.name inside lambda function is just index of current row.请注意, r.name function 中的 r.name 只是当前行的索引。

Another approach with no lambda at all:另一种完全没有 lambda 的方法:

(df['A.1'].sum()-df['A.1'])/(len(df)-1) - (df['A.2'].sum()-df['A.2'])/(len(df)-1)

result is the same as above.结果和上面一样。

暂无
暂无

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

相关问题 逐行计算pandas数据框中的新列 - Calculation new column in pandas dataframe from row by row calculation 如何在 Pandas Python 中的 DataFrame 中的其他 2 列中创建新列并计算日期之间的天数? - How to create new column with calculation of days between date in 2 other columns in DataFrame in Pandas Python? 对除第一行之外的每一行执行计算 - Pandas - Perform Calculation on Every Row, Except The First - Pandas 创建一个新行,它是上述行的计算结果 - Pandas DataFrame - Create a new row that is the result of a calculation of rows above - Pandas DataFrame 使用pandas数据框中的多个行或列值进行计算 - using more than one row or column value in a pandas dataframe for a calculation Pandas.DataFrame:创建一个新列,使用当前df中的一列并在另一个df中查找一列,并进行计算 - Pandas.DataFrame: Create a new column, using one column from current df and by looking up one column in another df, with calculation 如何在 Pandas Data Frame 中跨其他行的计算中创建新行? - How to create new row out of calculation across other rows in Pandas Data Frame? 日期时间计算中的新 pandas DataFrame 列 - New pandas DataFrame column from datetime calculation 如何通过包含一列某些值的平均值的计算在 DataFrame 中创建新列 - How to create a new column in a DataFrame from a calculation that includes the mean of some values of one column 熊猫数据框列计算 - Pandas dataframe column calculation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM