【发布时间】:2022-01-05 15:35:46
【问题描述】:
我的目标是使用 my_function 更改数据帧 df,然后将结果分配给数据帧 df。 但是当我使用函数时,函数外部的数据帧 df 会发生变化。 如何修改函数而不影响函数之外的 df 变量?
import pandas as pd
df = pd.DataFrame({'A': [10, 20, 30]}, index=['2021-11-24', '2021-11-25', '2021-11-26'])
def my_function(df_temp):
df_temp['A'][0] = 100 # How could I modify not to affect df varable which is outside of funtion
return df_temp
something = my_function(df)
print(df) # df is already altered although I didn't assign
# df = my_function(df)
# print(df)
【问题讨论】:
标签: python function pass-by-reference pass-by-value