loc()函数

1 loc()函数用于定位dataframe的某些行,可以进行切片操作,当只定位一行时,数据类型是series.

有时想要按dataframe中的某一列按条件进行索引时,可以将该列设置为索引列,即可用loc函数进行索引定位.

import pandas as pd
df = pd.DataFrame([
            ['green', 'M', 10.1, 'class1'],
            ['red', 'L', 13.5, 'class2'],
            ['blue', 'XL', 15.3, 'class1']],
                 columns=['a','b','c','d'])
print(df)
print(df.loc[2][1])
print(type(df.loc[2]))
print(df.loc[:,'a'])
#        a   b     c       d
# 0  green   M  10.1  class1
# 1    red   L  13.5  class2
# 2   blue  XL  15.3  class1
# XL
# <class 'pandas.core.series.Series'>
# 0    green
# 1      red
# 2     blue
# Name: a, dtype: object

猜你喜欢

转载自www.cnblogs.com/xxswkl/p/11015799.html