真正复制 pandas 系列 [英] make a truly deep copy of a pandas Series
本文介绍了真正复制 pandas 系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个pd.Series
,每个单元格都是一个列表.
我想对其做一个深拷贝,但是它看起来像
I have a pd.Series
with each cell being a list.
I want to make a deep copy of it, however it seems like pd.Series.copy
only creates a shallow copy of the values (even though the deep
arg is True
be default).
示例
import pandas as pd
sr = pd.Series([list(range(3)), list(range(3))])
sr_c = sr.copy()
sr[0].append(4)
复制的pd.Series
sr_c
正在转换为
0 [0, 1, 2, 4]
1 [0, 1, 2]
我做到了,而且奏效了:
I did this and it worked:
from copy import deepcopy
sr_c = sr_c.apply(deepcopy)
这似乎是一种hack,有没有更好的方法呢?
however this seems like a hack, is there a better way to do it ?
推荐答案
深度复制python对象的标准 hacky 方法应该可以使用.也就是说,使用pickle
.
The standard hacky way of deep-copying python objects should work. That is, using pickle
.
import pickle
sr2 = pickle.loads(pickle.dumps(sr))
这篇关于真正复制 pandas 系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文