pandas DataFrame 行列索引及值的获取的方法
作者:诗&远方 时间:2022-03-24 06:14:11
pandas DataFrame是二维的,所以,它既有列索引,又有行索引
上一篇里只介绍了列索引:
import pandas as pd
df = pd.DataFrame({'A': [0, 1, 2], 'B': [3, 4, 5]})
print df
# 结果:
A B
0 0 3
1 1 4
2 2 5
行索引自动生成了 0,1,2
如果要自己指定行索引和列索引,可以使用 index 和 column 参数:
这个数据是5个车站10天内的客流数据:
ridership_df = pd.DataFrame(
data=[[ 0, 0, 2, 5, 0],
[1478, 3877, 3674, 2328, 2539],
[1613, 4088, 3991, 6461, 2691],
[1560, 3392, 3826, 4787, 2613],
[1608, 4802, 3932, 4477, 2705],
[1576, 3933, 3909, 4979, 2685],
[ 95, 229, 255, 496, 201],
[ 2, 0, 1, 27, 0],
[1438, 3785, 3589, 4174, 2215],
[1342, 4043, 4009, 4665, 3033]],
index=['05-01-11', '05-02-11', '05-03-11', '05-04-11', '05-05-11',
'05-06-11', '05-07-11', '05-08-11', '05-09-11', '05-10-11'],
columns=['R003', 'R004', 'R005', 'R006', 'R007']
)
data 参数为一个numpy二维数组, index 参数为行索引, column 参数为列索引
生成的数据以表格形式显示:
R003 R004 R005 R006 R007
05-01-11 0 0 2 5 0
05-02-11 1478 3877 3674 2328 2539
05-03-11 1613 4088 3991 6461 2691
05-04-11 1560 3392 3826 4787 2613
05-05-11 1608 4802 3932 4477 2705
05-06-11 1576 3933 3909 4979 2685
05-07-11 95 229 255 496 201
05-08-11 2 0 1 27 0
05-09-11 1438 3785 3589 4174 2215
05-10-11 1342 4043 4009 4665 3033
下面说下如何获取DataFrame里的值:
1.获取某一列: 直接 ['key']
print(ridership_df['R003'])
# 结果:
05-01-11 0
05-02-11 1478
05-03-11 1613
05-04-11 1560
05-05-11 1608
05-06-11 1576
05-07-11 95
05-08-11 2
05-09-11 1438
05-10-11 1342
Name: R003, dtype: int64
2.获取某一行: .loc['key']
print(ridership_df.loc['05-01-11'])
# 或者
print(ridership_df.iloc[0])
# 结果:
R003 0
R004 0
R005 2
R006 5
R007 0
Name: 05-01-11, dtype: int64
3.获取某一行某一列的某个值:
print(ridership_df.loc['05-05-11','R003'])
# 或者
print(ridership_df.iloc[4,0])
# 结果:
1608
4.获取原始的numpy二维数组:
print(ridership_df.values)
# 结果:
[[ 0 0 2 5 0]
[1478 3877 3674 2328 2539]
[1613 4088 3991 6461 2691]
[1560 3392 3826 4787 2613]
[1608 4802 3932 4477 2705]
[1576 3933 3909 4979 2685]
[ 95 229 255 496 201]
[ 2 0 1 27 0]
[1438 3785 3589 4174 2215]
[1342 4043 4009 4665 3033]]
*注意在这过程中,数据格式如果不一致,会发生转换.
一个综合栗子:
从 ridership_df 找出第一天里客流量最多的车站,然后返回这个车站的日平均客流,以及返回所有车站的平均日客流,作为对比:
def mean_riders_for_max_station(ridership):
max_index = ridership.iloc[0].argmax()
mean_for_max = ridership[max_index].mean()
overall_mean = ridership.values.mean()
return (overall_mean, mean_for_max)
print mean_riders_for_max_station(ridership_df)
# 结果:
(2342.6, 3239.9)
来源:https://www.cnblogs.com/liulangmao/p/9248930.html
标签:pandas,DataFrame,行列索引,值获取
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Ubuntu安装Jupyter Notebook教程
2021-03-12 03:14:58
![](https://img.aspxhome.com/file/2023/5/77845_0s.png)
Python入门教程 超详细1小时学会Python
2023-06-13 23:34:12
![](https://img.aspxhome.com/file/2023/1/59131_0s.gif)
python 计算一个字符串中所有数字的和实例
2022-07-19 01:42:33
Python WebSocket长连接心跳与短连接的示例
2022-08-20 04:05:15
Python实现双人五子棋对局
2022-12-26 04:41:39
![](https://img.aspxhome.com/file/2023/7/65917_0s.jpg)
详解Python绘图Turtle库
2021-11-29 05:42:06
![](https://img.aspxhome.com/file/2023/3/65903_0s.png)
全方位清理浮动
2009-06-16 14:51:00
推荐一篇不错的新手asp编程的基本法则
2011-04-15 11:08:00
判断浏览器是否接受 Cookie
2009-07-28 17:52:00
SQL Server 2000 占内存居高不下可能的原因及其解决方法
2010-04-25 10:39:00
微信跳一跳自动运行python脚本
2023-11-22 01:42:29
Python Pandas创建Dataframe数据框的六种方法汇总
2023-08-25 07:39:16
![](https://img.aspxhome.com/file/2023/2/68442_0s.png)
与 Function 和 Object 相关的有趣代码
2010-05-07 12:29:00
![](https://img.aspxhome.com/file/UploadPic/20105/7/javascript_object_layout-52s.jpg)
有故事的网页设计——Flash网站奇妙之旅
2011-01-20 19:58:00
![](https://img.aspxhome.com/file/UploadPic/20111/20/a1-39s.jpg)
FckEditor配置手册中文教程详细说明
2010-02-28 12:37:00
Python爬取视频(其实是一篇福利)过程解析
2021-09-21 17:52:11
如何使用ASP实现网站的“目录树”管理
2008-06-13 06:39:00
如何获知文件被改动的情况?
2009-11-24 20:42:00
JavaScript大牛:Douglas Crockford
2009-03-31 12:06:00
![](https://img.aspxhome.com/file/UploadPic/20093/31/yui-team-14s.jpg)
PL/SQL编程经验小结开发者网络Oracle
2010-07-18 13:27:00