首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

python大法好,这些你可不要忽略了!

1. loc——通过行标签索引行数据

1.1 loc[1]表示索引的是第1行(index 是整数)

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = [0,1]

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.loc[1]

'''''

a 4

b 5

c 6

'''

1.2 loc[‘d’]表示索引的是第’d’行(index 是字符)

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.loc['d']

'''''

a 1

b 2

c 3

'''

1.3 如果想索引列数据,像这样做会报错

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.loc['a']

'''''

KeyError: 'the label [a] is not in the [index]'

'''

1.4 loc可以获取多行数据

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.loc['d':]

'''''

a b c

d 1 2 3

e 4 5 6

'''

1.5 loc扩展——索引某行某列

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.loc['d',['b','c']]

'''''

b 2

c 3

'''

1,6 loc扩展——索引某列

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.loc[:,['c']]

'''''

c

d 3

e 6

'''

当然获取某列数据最直接的方式是df.[列标签],但是当列标签未知时可以通过这种方式获取列数据。

2. iloc——通过行号获取行数据

2.1 想要获取哪一行就输入该行数字

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.loc[1]

'''''

a 4

b 5

c 6

'''

2.2 通过行标签索引会报错

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.iloc['a']

'''''

'''

2.3 同样通过行号可以索引多行

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.iloc[0:]

'''''

a b c

d 1 2 3

e 4 5 6

'''

2.4 iloc索引列数据

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.iloc[:,[1]]

'''''

b

d 2

e 5

'''

3. ix——结合前两种的混合索引

3.1 通过行号索引

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.ix[1]

'''''

a 4

b 5

c 6

'''

3.2 通过行标签索引

1. loc——通过行标签索引行数据

1.1 loc[1]表示索引的是第1行(index 是整数)

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = [0,1]

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.loc[1]

'''''

a 4

b 5

c 6

'''

1.2 loc[‘d’]表示索引的是第’d’行(index 是字符)

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.loc['d']

'''''

a 1

b 2

c 3

'''

1.3 如果想索引列数据,像这样做会报错

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.loc['a']

'''''

KeyError: 'the label [a] is not in the [index]'

'''

1.4 loc可以获取多行数据

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.loc['d':]

'''''

a b c

d 1 2 3

e 4 5 6

'''

1.5 loc扩展——索引某行某列

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.loc['d',['b','c']]

'''''

b 2

c 3

'''

1,6 loc扩展——索引某列

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.loc[:,['c']]

'''''

c

d 3

e 6

'''

当然获取某列数据最直接的方式是df.[列标签],但是当列标签未知时可以通过这种方式获取列数据。

需要注意的是,dataframe的索引[1:3]是包含1,2,3的,与平时的不同。

2. iloc——通过行号获取行数据

2.1 想要获取哪一行就输入该行数字

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.loc[1]

'''''

a 4

b 5

c 6

'''

2.2 通过行标签索引会报错

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.iloc['a']

'''''

'''

2.3 同样通过行号可以索引多行

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.iloc[0:]

'''''

a b c

d 1 2 3

e 4 5 6

'''

2.4 iloc索引列数据

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.iloc[:,[1]]

'''''

b

d 2

e 5

'''

3. ix——结合前两种的混合索引

3.1 通过行号索引

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.ix[1]

'''''

a 4

b 5

c 6

'''

3.2 通过行标签索引

[python] iew plain copy

import pandas as pd

data = [[1,2,3],[4,5,6]]

index = ['d','e']

columns=['a','b','c']

df = pd.DataFrame(data=data, index=index, columns=columns)

print df.ix['e']

'''''

a 4

b 5

c 6

'''

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180301A0V6KK00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券