[英]What are the pros/cons in using pd.Index vs df.loc
使用pd.Index
与df.loc
什么区别? 它实际上是同一件事吗?
idx = pd.Index(('a', 'b'))
df = pd.DataFrame({'a': [0, 1], 'b': [2, 3], 'c': [0, 5]})
print(df.loc[:, ('a', 'b')],)
print(df[idx])
a b
0 0 2
1 1 3
当您执行loc
时,您可以使用索引切片和列切片或组合,但是pd.index
只能用于列切片
df.loc[[0]]
a b c
0 0 2 0
df.loc[[0],['a','b']]
a b
0 0 2
IMO, loc
使用起来更灵活,我将 select loc
从长远来看或检查后台会更清楚。
文档中描述了loc
如何是首选方法。 使用多个切片可能会导致SettingWithCopyWarning
:
idx = ['a', 'b']
d = df[idx]
d.iloc[0,0] = 9
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
相反,使用loc
不会触发SettingWithCopyWarning
:
idx = ['a', 'b']
d = df.loc[:,idx]
d.iloc[0,0] = 9
值得注意的是, loc
还允许您将特定轴作为参数传递:
df.loc(axis=1)[idx]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.