新闻动态

Pandas读取行列数据最全方法

发布日期:2022-02-12 15:17 | 文章来源:CSDN

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读取行列 内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

香港服务器租用

版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。

相关文章

实时开通

自选配置、实时开通

免备案

全球线路精选!

全天候客户服务

7x24全年不间断在线

专属顾问服务

1对1客户咨询顾问

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部