Pandas读取行列数据最全方法

作者:Sunshine168 时间:2022-06-23 09:34:22 

1、读取方法有按行(单行,多行连续,多行不连续),按列(单列,多列连续,多列不连续);部分不连续行不连续列;按位置(坐标),按字符(索引);按块(list);函数有 df.iloc(), df.loc(), df.iat(), df.at(), df.ix()。

2、转换为DF,赋值columns,index,修改添加数据,取行列索引


data = {'省份': ['北京', '上海', '广州', '深圳'],
       '年份': ['2017', '2018', '2019', '2020'],
       '总人数': ['2200', '1900', '2170', '1890'],
       '高考人数': ['6.3', '5.9', '6.0', '5.2']}
df = pd.DataFrame(data, columns=['省份', '年份', '总人数', '高考人数', '高数'],
                 index=['one', 'two', 'three', 'four'])
df['高数'] = ['90', '95', '92', '98']
print("行索引:{}".format(list(df.index)))
print("列索引:{}".format(list(df.columns)))
print(df.index[1:3])
print(df.columns[1])
print(df.columns[1:3])
print(df)

行索引:['one', 'two', 'three', 'four']
列索引:['省份', '年份', '总人数', '高考人数', '高数']
Index(['two', 'three'], dtype='object')
年份
Index(['年份', '总人数'], dtype='object')
       省份    年份   总人数 高考人数  高数
one    北京  2017  2200  6.3  90
two    上海  2018  1900  5.9  95
three  广州  2019  2170  6.0  92
four   深圳  2020  1890  5.2  98

3、iloc不能通过[:, [1:3]]取连续数据,取连续数据只能通过 df[df.columns[1:4]],先获取列索引,再取数据。


print(df['省份'])  #按列名取列
print(df.省份)  #按列名取列
print(df[['省份', '总人数']])  #按列名取不连续列数据
print(df[df.columns[1:4]])  #按列索引取连续列数据
print(df.iloc[:, 1])  #按位置取列
print(df.iloc[:, [1, 3]])  #按位置取不连续列数据

one      北京
two      上海
three    广州
four     深圳
Name: 省份, dtype: object
one      北京
two      上海
three    广州
four     深圳
Name: 省份, dtype: object
       省份   总人数
one    北京  2200
two    上海  1900
three  广州  2170
four   深圳  1890
         年份   总人数 高考人数
one    2017  2200  6.3
two    2018  1900  5.9
three  2019  2170  6.0
four   2020  1890  5.2
one      2017
two      2018
three    2019
four     2020
Name: 年份, dtype: object
         年份 高考人数
one    2017  6.3
two    2018  5.9
three  2019  6.0
four   2020  5.2

4、通过df.iloc[](数字)取行数据,取部分行部分列时,要先写行,再写列;有条件的取数据


print(df[1:3])  #按行取数据,这行代码结果没在下面输出
print(df[df.高数>90])  #按行有条件的取数据,结果没输出
print(df.iloc[1])  #按行取行数据
print(df.iloc[1, 3])  #按坐标取
print(df.iloc[[1], [3]])  #按坐标取
print(df.loc[df.index[1:3]])  #按行索引取行,但没必要
print(df.iloc[1:3])  #按行取连续数据
print(df.iloc[[1, 3]])  按行取不连续数据
print(df.iloc[[1,2,3], [2,4]])  取部分行部分列数据

省份        上海
年份      2018
总人数     1900
高考人数     5.9
高数        95
Name: two, dtype: object
5.9
    高考人数
two  5.9
       省份    年份   总人数 高考人数  高数
two    上海  2018  1900  5.9  95
three  广州  2019  2170  6.0  92
       省份    年份   总人数 高考人数  高数
two    上海  2018  1900  5.9  95
three  广州  2019  2170  6.0  92
      省份    年份   总人数 高考人数  高数
two   上海  2018  1900  5.9  95
four  深圳  2020  1890  5.2  98
        总人数  高数
two    1900  95
three  2170  92
four   1890  98

5、通过df.loc[]索引(字符)取行数据。


print(df.loc['two'])
print(df.loc['two', '省份'])
print(df.loc['two':'three'])
print(df.loc[['one', 'three']])
print(df.loc[['one', 'three'], ['省份', '年份']])

省份        上海
年份      2018
总人数     1900
高考人数     5.9
高数        95
Name: two, dtype: object
上海
       省份    年份   总人数 高考人数  高数
two    上海  2018  1900  5.9  95
three  广州  2019  2170  6.0  92
       省份    年份   总人数 高考人数  高数
one    北京  2017  2200  6.3  90
three  广州  2019  2170  6.0  92
       省份    年份
one    北京  2017
three  广州  2019

6、ix,iat,at取行列数据,此方法不常用,可以使用上面方法即可。


print(df.ix[1:3])
print(df.ix[:, [1, 3]])
print(df.iat[1,3])
print(df.at['two', '省份'])

       省份    年份   总人数 高考人数  高数
two    上海  2018  1900  5.9  95
three  广州  2019  2170  6.0  92
         年份 高考人数
one    2017  6.3
two    2018  5.9
three  2019  6.0
four   2020  5.2
5.9
上海

来源:https://www.cnblogs.com/wynlfd/p/14024947.html

标签:Pandas,读取,行,列
0
投稿

猜你喜欢

  • asp中的on error resume next用法

    2008-03-09 15:22:00
  • 客户端数据存储–超越cookies

    2008-01-15 13:01:00
  • python commands模块的适用方式

    2022-02-26 19:38:14
  • 10分钟用Python快速搭建全文搜索引擎详解流程

    2023-11-06 16:13:41
  • 归纳整理Python中的控制流语句的知识点

    2021-03-04 12:07:12
  • 学习python之编写简单乘法口诀表实现代码

    2021-11-12 20:19:20
  • ASP实现SQL语句日期格式的加减运算

    2008-11-07 15:13:00
  • .net 上传文件前所未有的简单

    2023-07-17 23:20:04
  • 解决Django中checkbox复选框的传值问题

    2023-09-11 17:15:22
  • Python字符串拼接的4种方法实例

    2023-01-30 18:57:15
  • Python实现将一个正整数分解质因数的方法分析

    2021-01-09 10:39:29
  • Django框架中模型的用法

    2022-02-15 00:34:14
  • 如何用SQL语句来建表?

    2010-06-13 14:38:00
  • numpy.insert()的具体使用方法

    2021-12-23 15:33:34
  • 运用ASP调用数据库中视图及存储过程

    2008-02-03 15:33:00
  • Python数据结构之二叉排序树的定义、查找、插入、构造、删除

    2022-03-03 08:19:51
  • 使用python检测主机存活端口及检查存活主机

    2021-08-01 05:20:27
  • 如何使用Typora+MinIO+Python代码打造舒适协作环境

    2023-11-12 15:12:10
  • Python运维自动化之nginx配置文件对比操作示例

    2023-08-04 16:54:29
  • Ubuntu 下 vim 搭建python 环境 配置

    2022-04-27 21:25:17
  • asp之家 网络编程 m.aspxhome.com