python - 将数据分配给数据框时如何发生以下情况
问题描述
我的理解是,在“=”操作数的情况下,信息从右向左流动。即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), 0, 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
fivminohlc 中怎么显示 Y 和 Y1
解决方案
因为 fivminohlc 是一个类的实例,所以当您将其分配给 df_main 时,df_main 本质上就变成了指向 fivminohlc 的“指针”。
df_main 和 fivminohlc 都代表同一个实例。因此,通过更新 df_main,您也在更新 fivminohlc。
class A:
num = 1
a = A()
b = a
b.num = 2
print(a.num)
print(a == b)
上面的代码将打印
2
True
请参阅此文档:https ://docs.python.org/3/tutorial/classes.html
第 9.3.5 节。类和实例变量也可能有用。
制作副本
文档:https ://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.copy.html
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)
这将打印:
Index(['Name', 'RoomNumber', 'NumberOfGuests'], dtype='object')
Index(['Name', 'RoomNumber'], dtype='object')
推荐阅读
- amazon-web-services - 如何识别程序访问用户使用的确切 IAM 用户权限
- regex - 正则表达式修改捕获组
- python - 如果另一个列列表具有所有数字字符的元素,则填充列
- linux - 用于检测终端中运行的最后一个命令的 Bash 脚本
- google-cloud-platform - Queries on GKE Autoscaling
- javascript - 如何在 Node JS JWT 身份验证中设置角色?
- spring - 在spring boot上将h2更改为mysql db得到错误
- amazon-web-services - 我想将文件从 S3 带到 Elasticsearch
- python - 将数组作为函数参数传递
- ansible - 如何将 GCP 项目加入 Ansible Tower 以在其中运行 playbook?