简体   繁体   English

df.loc[:, columns] 和 df.loc[:][columns] 的区别

[英]difference between df.loc[:, columns] and df.loc[:][columns]

I want to normalize some columns of a pandas data frame using MinMaxScaler in this way:我想以这种方式使用MinMaxScaler规范化熊猫数据框的某些列:

scaler = MinMaxScaler()
numericals = ["TX_TIME_SECONDS",'TX_Amount']

while I do in this way:虽然我这样做:

df.loc[:][numericals] = scaler.fit_transform(df.loc[:][numericals])

it's not done inplace and df is not changed;它没有就地完成, df也没有改变;

whereas, when I do in this way:而当我这样做时:

df.loc[:, numericals] = scaler.fit_transform(df.loc[:][numericals])

the numerical columns of df are changed in place, df的数字列就地更改,

So, What's the difference between df.loc[:, ~] and df.loc[:][~]那么, df.loc[:, ~]df.loc[:][~]什么区别

df.loc[:][numericals] selects all rows and then selects columns "TX_TIME_SECONDS" and 'TX_Amount' of the returning object , and assigns some value to it. df.loc[:][numericals]选择所有行,然后选择返回对象的列“TX_TIME_SECONDS”和“TX_Amount”,并为其分配一些值。 The problem is, the returning object might be a copy so this may not change the actual DataFrame.问题是,返回的对象可能是一个副本,因此这可能不会更改实际的 DataFrame。

The correct way of making this assignment is using df.loc[:, numericals] , because with .loc you are guaranteed to modify the original DataFrame.进行此分配的正确方法是使用df.loc[:, numericals] ,因为使用.loc您可以保证修改原始数据帧。

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

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