将数据分配给数据框时如何进行跟踪

我的理解是,对于“ =”操作数,信息从右向左流动。即a = b意味着b的值被转移到a。而且,如果我事后更改a,则不会影响b的值。但在下面的代码中,它正在发生。谁能告诉我为什么会这样吗?

df_main=fivminohlc

result=df_main.dtypes

print(result)

result=fivminohlc.dtypes

print(result)

O    float64
H    float64
L    float64
C    float64
V      int64
dtype: object
O    float64
H    float64
L    float64
C    float64
V      int64
dtype: object

df_main['Y1']=(df_main['C']-df_main['O'])/df_main['O'] # I have not touched fivminohlc

df_main['Y'] = np.where((df_main.Y1 > .001),2,1) 

df_main['Y'] = np.where((df_main.Y1 < -.001),1) 

result=df_main.dtypes

print(result)

result=fivminohlc.dtypes

print(result)

O     float64
H     float64
L     float64
C     float64
V       int64
Y1    float64
Y       int32
dtype: object
O     float64
H     float64
L     float64
C     float64
V       int64
Y1    float64
Y       int32
dtype: object

Y和Y1在fivminohlc中如何显示

yyuuzzjj 回答:将数据分配给数据框时如何进行跟踪

因为fivminohlc是类的实例,所以当您将其分配给df_main时,df_main本质上成为fivminohlc的“指针”。

df_main和fivminohlc都代表相同的实例。因此,通过更新df_main,您还将更新fivminohlc。

response.data.CreatePublisher

以上代码将打印

class A:
    num = 1

a = A()
b = a
b.num = 2
print(a.num)
print(a == b)

请参阅此文档:https://docs.python.org/3/tutorial/classes.html

第9.3.5节。类和实例变量也可能有用。

复制

文档:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.copy.html

2
True

这将打印:

from pandas import DataFrame

# Instantiate an initial dataframe with columns "Name" and "RoomNumber"
df = DataFrame(columns=["Name","RoomNumber"])

# Instantiate second_instance which effectively acts as a pointer to df's instance. 
# Also instantiate df_copy using df.copy() which copies the entirety of df into a
# new object.
second_instance = df
df_copy = df.copy()

# Update second_instance to add a new column,and print df. We can clearly see 
# that the change to second_instance affected df.
second_instance["NumberOfGuests"] = {}
print(df.columns)

# Now print df_copy. We can see that the above change to second_instance did not 
# affect df_copy as it is a separate instance.
print(df_copy.columns)

本文链接:https://www.f2er.com/3108826.html

大家都在问