pandas中常用的选择数据方法有:
-
loc:select by label
loc是根据行/列标签来进行选择的。 -
iloc:select by position
iloc是根据索引/下标来进行选择的。
例子
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(12).reshape(3, 4), index=['A', 'B', 'C'], columns=['a', 'b', 'c', 'd'])
print(df)
# a b c d
# A 0 1 2 3
# B 4 5 6 7
# C 8 9 10 11
print(df.loc['A']) # 选择行标签'A'
# a 0
# b 1
# c 2
# d 3
# Name: A, dtype: int64
print(df.iloc[0]) # 选择行索引'0'
# a 0
# b 1
# c 2
# d 3
# Name: A, dtype: int64
本文介绍了Pandas库中两种常用的数据选择方法:loc和iloc。loc允许通过行/列标签进行数据选择,而iloc则通过索引/下标进行选择。通过示例展示了如何使用这两种方法来选择DataFrame中的特定数据。

2803

被折叠的 条评论
为什么被折叠?



