Pandas怎么读取行列数据

发布时间:2021-08-13 10:14:55 作者:chen
来源:亿速云 阅读:139

这篇文章主要讲解了“Pandas怎么读取行列数据”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Pandas怎么读取行列数据”吧!

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
上海

感谢各位的阅读,以上就是“Pandas怎么读取行列数据”的内容了,经过本文的学习后,相信大家对Pandas怎么读取行列数据这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. 怎么在pandas 中使用DataFrame索引行列
  2. 如何对pandas的行列名进行更改与数据选择

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

pandas

上一篇:Vue插件如何使用

下一篇:Vue中响应式原理的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》