[英]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
您可以保证修改原始数据帧。
I suggest you read some documentation because this is pretty basic.我建议你阅读一些文档,因为这是非常基本的。
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html
https://www.geeksforgeeks.org/python-pandas-dataframe-loc/
https://www.geeksforgeeks.org/python-pandas-dataframe-loc/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.