Python Pandas 中的 .loc 是否对原始数据帧进行就地更改?
Does .loc in Python Pandas make inplace change on the original dataframe?
我正在处理如下数据框:
df:
Site Visits Temp Type
KFC 511 74 Food
KFC 565 77 Food
KFC 498 72 Food
K&G 300 75 Gas
K&G 255 71 Gas
我想将 'Type' 列更改为 0-1 变量,这样我就可以使用 df.corr() 来检查相关性。
我试了两种方法,一种是做字典,另做一个列:
dict = {'Food':1, 'Gas':0}
df['BinaryType'] = df['Type'].map(dict)
然后我可以使用 df.corr()
检查 'Visits' 和 'BinaryType' 之间的相关性。由于 'Type' 列包含字符串,因此 df.corr() 不会显示 'Visits' 和 'Type'.
之间的相关性
第二种方法是使用 .loc:
df.loc[df['Type']=='Food','Type'] = 1
df.loc[df['Type']!=1,'Type'] = 0
然后我在console中查看了df,如下图,好像是就地修改了。我还使用 df['Type'][0]
检查了数据类型,它显示为 1(我想它是整数):
Site Visits Temp Type
KFC 511 74 1
KFC 565 77 1
KFC 498 72 1
K&G 300 75 0
K&G 255 71 0
然而,df.corr()
不会显示 'Visits' 和 'Type' 之间的相关性!好像这个栏目没改过一样。
您可以使用下面的代码重现:
df = pd.DataFrame({
'Site': {0: 'KFC', 1: 'KFC', 2: 'KFC', 3: 'K&G', 4:'K&G'},
'Visits': {0: 511, 1: 565, 2: 498, 3: 300, 4:255},
'Temp': {0: 74, 1: 77, 2: 72, 3: 75, 4:71},
'Type': {0: 'Food', 1: 'Food', 2: 'Food', 3: 'Gas', 4:'Gas'}})
# 1
dict = {'Food':1, 'Gas':0}
df['BinaryType'] = df['Type'].map(dict)
df.corr()
del df['BinaryType']
# 2
df.loc[df['Type']=='Food','Type'] = 1
df.loc[df['Type']!=1,'Type'] = 0
df.corr()
知道 Pandas .loc 如何在后台工作吗?
你的第二种方法实际上并没有改变系列的dtype
,即使值都是整数。您可以通过执行 df.dtypes
看到 Type
列仍然是 object
dtype
您需要使用 .astype(int)
将它们显式转换为 int
或
使用df['Type'] = np.where(df['Type'] == 'Food', 1, 0)
运行 df.corr()
之后给出
In [22]: df.corr()
Out[22]:
Visits Temp Type
Visits 1.000000 0.498462 0.976714
Temp 0.498462 1.000000 0.305888
Type 0.976714 0.305888 1.000000
由于您的第一种方法有效,您可以使用:
dict = {'Food':1, 'Gas':0}
df['Type'] = df['Type'].map(dict)
我正在处理如下数据框:
df:
Site Visits Temp Type
KFC 511 74 Food
KFC 565 77 Food
KFC 498 72 Food
K&G 300 75 Gas
K&G 255 71 Gas
我想将 'Type' 列更改为 0-1 变量,这样我就可以使用 df.corr() 来检查相关性。
我试了两种方法,一种是做字典,另做一个列:
dict = {'Food':1, 'Gas':0}
df['BinaryType'] = df['Type'].map(dict)
然后我可以使用 df.corr()
检查 'Visits' 和 'BinaryType' 之间的相关性。由于 'Type' 列包含字符串,因此 df.corr() 不会显示 'Visits' 和 'Type'.
第二种方法是使用 .loc:
df.loc[df['Type']=='Food','Type'] = 1
df.loc[df['Type']!=1,'Type'] = 0
然后我在console中查看了df,如下图,好像是就地修改了。我还使用 df['Type'][0]
检查了数据类型,它显示为 1(我想它是整数):
Site Visits Temp Type
KFC 511 74 1
KFC 565 77 1
KFC 498 72 1
K&G 300 75 0
K&G 255 71 0
然而,df.corr()
不会显示 'Visits' 和 'Type' 之间的相关性!好像这个栏目没改过一样。
您可以使用下面的代码重现:
df = pd.DataFrame({
'Site': {0: 'KFC', 1: 'KFC', 2: 'KFC', 3: 'K&G', 4:'K&G'},
'Visits': {0: 511, 1: 565, 2: 498, 3: 300, 4:255},
'Temp': {0: 74, 1: 77, 2: 72, 3: 75, 4:71},
'Type': {0: 'Food', 1: 'Food', 2: 'Food', 3: 'Gas', 4:'Gas'}})
# 1
dict = {'Food':1, 'Gas':0}
df['BinaryType'] = df['Type'].map(dict)
df.corr()
del df['BinaryType']
# 2
df.loc[df['Type']=='Food','Type'] = 1
df.loc[df['Type']!=1,'Type'] = 0
df.corr()
知道 Pandas .loc 如何在后台工作吗?
你的第二种方法实际上并没有改变系列的dtype
,即使值都是整数。您可以通过执行 df.dtypes
看到 Type
列仍然是 object
dtype
您需要使用 .astype(int)
或
使用df['Type'] = np.where(df['Type'] == 'Food', 1, 0)
运行 df.corr()
之后给出
In [22]: df.corr()
Out[22]:
Visits Temp Type
Visits 1.000000 0.498462 0.976714
Temp 0.498462 1.000000 0.305888
Type 0.976714 0.305888 1.000000
由于您的第一种方法有效,您可以使用:
dict = {'Food':1, 'Gas':0}
df['Type'] = df['Type'].map(dict)