dataframe行列查询

一、可以通过df.shape查询行列数

import pandas as pd

df = pd.DataFrame([[1,2,3], [7,5,7], [9,7,4], [8,4,6]])
# 维度查询
>>> df.shape
(4, 3)

# 获取行数
>>> df.shape[0]
4

# 获取列数
>>> df.shape[1]
3

二、查询行列名

# 我们可以先修改一下行列名
df.columns = ['col1', 'col2', 'col3']
df.index = ['i1', 'i2', 'i3', 'i4']
>>> df
    col1  col2  col3
i1     1     2     3
i2     7     5     7
i3     9     7     4
i4     8     4     6

# 获取列名
>>> df.columns
Index(['col1', 'col2', 'col3'], dtype='object')
# 获取行名
>>> df.index
Index(['i1', 'i2', 'i3', 'i4'], dtype='object')

三、获取某行或某列

# 获取某行
df.loc['i1']
col1    1
col2    2
col3    3
Name: i1, dtype: int64

# 获取某几行
>>> df.loc['i1':'i3']
    col1  col2  col3
i1     1     2     3
i2     7     5     7
i3     9     7     4

# 获取某列
>>> df['col1']
i1    1
i2    7
i3    9
i4    8
Name: col1, dtype: int64

# 获取某几列
>>> df[['col1','col2']]
    col1  col2
i1     1     2
i2     7     5
i3     9     7
i4     8     4

# 也可用查询行的方法获取列
>>> df.loc[:, ['col1', 'col2']]
    col1  col2
i1     1     2
i2     7     5
i3     9     7
i4     8     4

 

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值