<万博manbetx平台>Pandas读取行列数据最全方法 - 万博manbetx平台中文网

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

以上就是Pandas读取行列数据最全方法的详细内容,更多请关注万博manbetx平台中文网其它相关文章!

赞(0) 打赏
未经允许不得转载:万博manbetx平台中文网首页 » python

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏