Pandas 複製 DataFrame

  1. pandas.DataFrame.copy() 方法語法
  2. 使用 pandas.DataFrame.copy() 方法複製 Pandas DataFrame
  3. 將 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

相關文章 - Pandas DataFrame

  • 如何將 Pandas DataFrame 列標題獲取為列表
  • 如何刪除 Pandas DataFrame 列
  • 如何在 Pandas 中將 DataFrame 列轉換為日期時間
  • 如何在 Pandas DataFrame 中將浮點數轉換為整數