python - 包含 python 对象(例如列表)的 Deepcopy pandas DataFrame

标签 python python-3.x pandas memory-management

需要帮助理解变量赋值、指针......

以下是可重现的。

import pandas as pd

df = pd.DataFrame({
    'listData': [
        ['c', 'f', 'd', 'a', 'e', 'b'], 
        [5, 2, 1, 4, 3]
    ]})

df['listDataSort'] = df['listData']

给出:

             listData        listDataSort
0  [c, f, d, a, e, b]  [c, f, d, a, e, b]
1     [5, 2, 1, 4, 3]     [5, 2, 1, 4, 3]

如果我只想对 listDataSort 列中的列表进行排序,我可能会尝试:

df['listDataSort'].apply(lambda l: l.sort())
df

但是,这会就地对两列中的列表进行排序。

             listData        listDataSort
0  [a, b, c, d, e, f]  [a, b, c, d, e, f]
1     [1, 2, 3, 4, 5]     [1, 2, 3, 4, 5]

我可以通过以下方式解决此问题:

df = pd.DataFrame({
    'listData': [
        ['c', 'f', 'd', 'a', 'e', 'b'], 
        [5, 2, 1, 4, 3]
    ]})

df['listDataSort'] = df['listData'].apply(sorted)

给予:

             listData        listDataSort
0  [c, f, d, a, e, b]  [a, b, c, d, e, f]
1     [5, 2, 1, 4, 3]     [1, 2, 3, 4, 5]

将 df 分配给不同的变量,比如 df2 仍然会将所有内容更改回原始源列表。此外,如何基于现有数据框创建新数据框,以便在不对现有数据框进行相同更改的情况下对新数据框进行更改?

df = pd.DataFrame({
    'listData': [
        ['c', 'f', 'd', 'a', 'e', 'b'], 
        [5, 2, 1, 4, 3]
    ]})

df2 = df
print('\ndf\n', df)
print('\ndf2\n', df2)

df2['listDataSort'] = df2['listData']
print('\ndf\n', df)
print('\ndf2\n', df2)

df2['listDataSort'].apply(lambda l: l.sort())
print('\ndf\n', df)
print('\ndf2\n', df2)

打印:

df
             listData
0  [c, f, d, a, e, b]
1     [5, 2, 1, 4, 3]

df2
             listData
0  [c, f, d, a, e, b]
1     [5, 2, 1, 4, 3]

df
             listData        listDataSort
0  [c, f, d, a, e, b]  [c, f, d, a, e, b]
1     [5, 2, 1, 4, 3]     [5, 2, 1, 4, 3]

df2
             listData        listDataSort
0  [c, f, d, a, e, b]  [c, f, d, a, e, b]
1     [5, 2, 1, 4, 3]     [5, 2, 1, 4, 3]

df
             listData        listDataSort
0  [a, b, c, d, e, f]  [a, b, c, d, e, f]
1     [1, 2, 3, 4, 5]     [1, 2, 3, 4, 5]

df2
             listData        listDataSort
0  [a, b, c, d, e, f]  [a, b, c, d, e, f]
1     [1, 2, 3, 4, 5]     [1, 2, 3, 4, 5]

还有:

df = pd.DataFrame({
    'listData': [
        ['c', 'f', 'd', 'a', 'e', 'b'], 
        [5, 2, 1, 4, 3]
    ]})
print('\ndf\n', df)

df3 = df
df3['listDataSort'] = df3['listData'].apply(sorted)
print('\ndf\n', df)
print('\ndf3\n', df3)

打印:

df
             listData
0  [c, f, d, a, e, b]
1     [5, 2, 1, 4, 3]

df
             listData        listDataSort
0  [c, f, d, a, e, b]  [a, b, c, d, e, f]
1     [5, 2, 1, 4, 3]     [1, 2, 3, 4, 5]

df3
             listData        listDataSort
0  [c, f, d, a, e, b]  [a, b, c, d, e, f]
1     [5, 2, 1, 4, 3]     [1, 2, 3, 4, 5]

最佳答案

当你运行时

df['listDataSort'] = df['listData']

您所做的只是将列表的引用 复制到新列。这意味着仅执行浅拷贝并且两列都引用相同的列表。因此,对一列的任何更改都可能会影响另一列。

您可以将列表推导式与 sorted 一起使用,它会返回数据的副本。这对您来说应该是最简单的选择。

df['listDataSort'] = [sorted(x) for x in df['listDataSort']]
df

             listData        listDataSort
0  [c, f, d, a, e, b]  [a, b, c, d, e, f]
1     [5, 2, 1, 4, 3]     [1, 2, 3, 4, 5]

现在,当谈到复制整个 DataFrame 的问题时,事情就有点复杂了。我会推荐 deepcopy:

import copy
df2 = df.apply(copy.deepcopy)

关于python - 包含 python 对象(例如列表)的 Deepcopy pandas DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55031427/

相关文章:

python - 在 pygame 中旋转图像,使其看起来像在旋转

python - 如何使用 PyGame 计时器事件?如何使用计时器将时钟添加到 pygame 屏幕?

python - httplib.BadStatusLine : '' on Linux but not Mac

python - 在导入中绕过 Python 导入的方法?

Python CSV 阅读器类型错误 : string pattern on bytes object

python - Pandas:将多列子集映射到单列子集的有效方法

python - 根据第 2 列的条件,使用第 1 列的输入创建新的 df 列

python - 在Python中用范围分割字符串

python - "TypeError: a bytes-like object is required, not ' str '"将压缩的 DICOM 卷读入 numpy 数组

python - 如何使用 python pandas 对分割文本进行分组并计算其数量?