首页 > 解决方案 > 有没有一种简单的方法可以锁定熊猫数据框中的某些列不被操纵?

问题描述

我想对除需要保持不变的一对之外的每一列应用一个函数。我现在这样做的方式:

  1. 将 xxx 列分配给变量
  2. 从 df 中删除 xxx 列
  3. 对df做一些操作
  4. 将变量合并到 df

例子:

cobId = combined.Id
cobSale = combined.SalePrice
combined = combined.drop(['Id', 'SalePrice'], axis = 1)
combined=(combined-combined.mean())/combined.std()
combined['Id'] = cobId
combined['SalePrice'] = cobSale

这里如何改进?

标签: pythonpandas

解决方案


使用pd.Index.difference

cols = combined.columns.difference(['Id','SalePrice'])
combined[cols] = combined[cols].sub(combined[cols].mean()).div(combined[cols].std())
print(combined)

这是一个例子:

df = pd.DataFrame({'col1':[1,2],'col2':[3,4]})
print(df)
   col1  col2
0     1     3
1     2     4

cols = df.columns.difference(['col1'])
df[cols] = df[cols].sub(df[cols].mean()).div(df[cols].std())

print(df)

我们还可以使用DataFrame.update

df2=df.drop(axis=1,labels='col1')
#df2=df[df.columns.difference(['col1'])]
df2 = df2.sub(df2.mean()).div(df2.std())
df.update(df2)
print(df)

输出:

   col1      col2
0     1 -0.707107
1     2  0.707107

推荐阅读