的就地版本数据帧.head()大Pandas

2024-04-24 17:25:16 发布

您现在位置:Python中文网/ 问答频道 /正文

熊猫体内是否有df.head(max_rows)的就地版本?你知道吗

当数据帧中的行太多而无法处理时,我需要限制它们的数量。你知道吗

目前我正在做df = df.head(10000000),但我认为这是内存效率低下。你知道吗


Tags: 数据内存版本df数量headmaxrows
1条回答
网友
1楼 · 发布于 2024-04-24 17:25:16

您可以将^{}用于就地操作:

n = 10000000
df.drop(df.index[n:], inplace=True)

但这可能无济于事。根据@unutbu's comment

df.drop(..., inplace=True) does modify df inplace, but due to the way inplace operations are implemented in Pandas, there is no real advantage to doing this over the more straight-forward reassignment to variable names. Personally I prefer functions that return values over functions that modify values, since with the former the assignment syntax makes it utterly clear what is getting modified.

这将在Jeff's answer中进一步解释。你知道吗

此外,请注意,此方法不适用于重复索引。你知道吗

相关问题 更多 >