使用df设置数据帧的值.Loc更新数据框架的副本


import pandas as pd
#Inserting some data into name_df 
list_name = ["Al","Brad"]  
list_age = [20,30]
name_df = pd.DataFrame(list_name,columns=["name"])  
age_df = pd.DataFrame(list_age,columns=["age"])
name_df = pd.concat([name_df,age_df],axis=1)
**#Make a copy of the dataframe**  
copy_df = name_df  
**#Editing the data in name_df**  
name_df.loc[name_df["name"]=="Brad", ["age"]] = 35
**#Change appears in copy_df**  
print(copy_df)

这应该发生吗?

复制。(copy.deepcopy())

import pandas as pd
import copy 
list_name = ["Al","Brad"]  
list_age = [20,30]
name_df = pd.DataFrame(list_name,columns=["name"])  
age_df = pd.DataFrame(list_age,columns=["age"])
name_df = pd.concat([name_df,age_df],axis=1)
copy_df = copy.deepcopy(name_df)  
name_df.loc[name_df["name"]=="Brad", ["age"]] = 35
print(copy_df)

相关内容

  • 没有找到相关文章

最新更新