将Pandas DataFrame切片为新的DataFrame [英] Slicing a Pandas DataFrame into a new DataFrame
问题描述
我想用布尔索引对DataFrame进行切片以获取副本,然后独立于原始DataFrame在该副本上执行操作.
I would like to slice a DataFrame with a Boolean index obtaining a copy, and then do stuff on that copy independently of the original DataFrame.
从此答案来看,使用.loc
使用布尔数组选择时,我会得到一个副本,但是,如果我尝试更改副本,则SettingWithCopyWarning
会妨碍您.那么这将是正确的方法吗?
Judging from this answer, selecting with .loc
using a Boolean array will hand me back a copy, but then, if I try to change the copy, SettingWithCopyWarning
gets in the way. Would this then be the correct way:
import numpy as np
import pandas as pd
d1 = pd.DataFrame(np.random.randn(10, 5), columns=['a', 'b', 'c', 'd', 'e'])
# create a new dataframe from the sliced copy
d2 = pd.DataFrame(d1.loc[d1.a > 1, :])
# do stuff with d2, keep d1 unchanged
推荐答案
您需要 copy
和 boolean indexing
,不需要新的DataFrame
构造函数:
d2 = d1[d1.a > 1].copy()
警告说明:
如果稍后在d2
中修改值,您会发现修改不会传播回原始数据(d1
),并且Pandas会发出警告.
If you modify values in d2
later you will find that the modifications do not propagate back to the original data (d1
), and that Pandas does warning.
这篇关于将Pandas DataFrame切片为新的DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!