Pandas 複製 DataFrame
Suraj Joshi
2023年1月30日
2021年1月22日
-
pandas.DataFrame.copy()
方法語法 -
使用
pandas.DataFrame.copy()
方法複製 Pandas DataFrame - 將 Pandas DataFrame 分配給變數來複制 DataFrame
本教程將介紹我們如何使用 DataFrame.copy()
方法複製一個 DataFrame 物件。
import pandas as pd
items_df = pd.DataFrame({
'Id': [302, 504, 708],
'Cost': ["300", "400", "350"],
})
print(items_df)
輸出:
Id Cost
0 302 300
1 504 400
2 708 350
我們將用上面的例子來演示如何在 Pandas 中使用 DataFrame.copy()
方法。
pandas.DataFrame.copy()
方法語法
DataFrame.copy(deep=True)
它返回 DataFrame
的副本。deep
預設為 True
,這意味著在副本中所作的任何更改將不會反映在原始 DataFrame 中。但是,如果我們設定 deep=False
,那麼在副本中所做的任何改變也會反映在原始 DataFrame 中。
使用 pandas.DataFrame.copy()
方法複製 Pandas DataFrame
import pandas as pd
import numpy as np
items_df = pd.DataFrame({
'Id': [302, 504, 708],
'Cost': ["300", "400", "350"],
})
deep_copy = items_df.copy()
print("Original DataFrame before changing value in copy DataFrame:")
print(items_df, "\n")
print("Copy DataFrame before changing value in copy DataFrame:")
print(deep_copy, "\n")
deep_copy.loc[0, "Cost"] = np.nan
print("Original DataFrame after changing value in copy DataFrame:")
print(items_df, "\n")
print("Copy DataFrame after changing value in copy DataFrame:")
print(deep_copy, "\n")
輸出:
Original DataFrame before changing value in copy DataFrame:
Id Cost
0 302 300
1 504 400
2 708 350
Copy DataFrame before changing value in copy DataFrame:
Id Cost
0 302 300
1 504 400
2 708 350
Original DataFrame after changing value in copy DataFrame:
Id Cost
0 302 300
1 504 400
2 708 350
Copy DataFrame after changing value in copy DataFrame:
Id Cost
0 302 NaN
1 504 400
2 708 350
它建立了 DataFrame items_df
的副本作為 deep_copy
。如果我們改變了副本 deep_copy
的任何值,原來的 DataFrame items_df
就沒有變化。我們在 deep_copy
中把第一行的 Cost
列的值設定為 NaN
,但 items_df
卻沒有變化。
將 Pandas DataFrame 分配給變數來複制 DataFrame
import pandas as pd
import numpy as np
items_df = pd.DataFrame({
'Id': [302, 504, 708],
'Cost': ["300", "400", "350"],
})
copy_cost = items_df["Cost"]
print("Cost column of Original DataFrame before changing value in copy DataFrame:")
print(items_df, "\n")
print("Cost column of Copied DataFrame before changing value in copy DataFrame:")
print(copy_cost, "\n")
copy_cost[0] = np.nan
print("Cost column of Original DataFrame after changing value in copy DataFrame:")
print(copy_cost, "\n")
print("Cost column of Copied DataFrame after changing value in copy DataFrame:")
print(copy_cost, "\n")
輸出:
Cost column of Original DataFrame before changing value in copy DataFrame:
Id Cost
0 302 300
1 504 400
2 708 350
Cost column of Copied DataFrame before changing value in copy DataFrame:
0 300
1 400
2 350
Name: Cost, dtype: object
Cost column of Original DataFrame after changing value in copy DataFrame:
0 NaN
1 400
2 350
Name: Cost, dtype: object
Cost column of Copied DataFrame after changing value in copy DataFrame:
0 NaN
1 400
2 350
Name: Cost, dtype: object
它將 DataFrame items_df
中的 Cost
列建立為 copy_cost
。
Author: Suraj Joshi
Suraj Joshi is a backend software engineer at Matrice.ai.
LinkedIn