以下代码是我的简单示例,对于每一步,我都会在评论中解释我在做什么,问题在最后。
import pandas as pd
todays_date = datetime.datetime.now().date()
index = pd.date_range(todays_date, periods=3, freq='D')
columns = ['A','B','C']
df1 = pd.DataFrame(index=index, columns=columns)
df1 = df1.fillna(1)
# up to here, i've just create a random df1, which looks like the follow:
# A B C
# 2020-03-24 1 1 1
# 2020-03-25 1 1 1
# 2020-03-26 1 1 1
df2 = df1 # here created a copy of df1 named as df2, it should pass by value based on my knowledge
df1 += df2.shift(1) # this should be the same as df1 = df1 + df2.shift(1)
display(df1) # here I print out df1 which looks like the follow:
# A B C
# 2020-03-24 NaN NaN NaN
# 2020-03-25 2.0 2.0 2.0
# 2020-03-26 2.0 2.0 2.0
display(df2) # here I print out df2, the result surprise me because i thought df2 isn't changed from when it is defined , however it becomes the same as the new df1:
# A B C
# 2020-03-24 NaN NaN NaN
# 2020-03-25 2.0 2.0 2.0
# 2020-03-26 2.0 2.0 2.0
Run Code Online (Sandbox Code Playgroud)
谁能向我解释为什么df2在这些步骤中发生了变化?我真的很困惑。
Run Code Online (Sandbox Code Playgroud)df2 = df1 # here created a copy of df1 named as df2
该评论不正确,可能是您误解的原因。
这一行的意思是:df2现在是目前已知的任何东西的另一个名称df1。
因此,如果更改称为 的对象,则df1在引用 时也会看到此更改df2。