Pandas MultiIndex(多重索引)
来源:互联网 发布:生意软件 编辑:程序博客网 时间:2024/06/18 13:27
创建多重索引
In [16]: df = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=index)In [17]: dfOut[17]: first bar baz foo qux \second one two one two one two one A 0.895717 0.805244 -1.206412 2.565646 1.431256 1.340309 -1.170299 B 0.410835 0.813850 0.132003 -0.827317 -0.076467 -1.187678 1.130127 C -1.413681 1.607920 1.024180 0.569605 0.875906 -2.211372 0.974466 first second two A -0.226169 B -1.436737 C -2.006747
获得索引信息
get_level_values
In [23]: index.get_level_values(0)Out[23]: Index(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], dtype='object', name='first')In [24]: index.get_level_values('second')Out[24]: Index(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'], dtype='object', name='second')
基本索引
In [25]: df['bar']Out[25]: second one twoA 0.895717 0.805244B 0.410835 0.813850C -1.413681 1.607920In [26]: df['bar', 'one']Out[26]: A 0.895717B 0.410835C -1.413681Name: (bar, one), dtype: float64In [27]: df['bar']['one']Out[27]: A 0.895717B 0.410835C -1.413681Name: one, dtype: float64
使用reindex对齐数据
数据准备
In [11]: s = pd.Series(np.random.randn(8), index=arrays)In [12]: sOut[12]: bar one -0.861849 two -2.104569baz one -0.494929 two 1.071804foo one 0.721555 two -0.706771qux one -1.039575 two 0.271860dtype: float64
s序列加(0~-2)索引的值,因为s[:-2]没有最后两个的索引,所以为NaN.s[::2]意思是步长为1.
In [34]: s + s[:-2]Out[34]: bar one -1.723698 two -4.209138baz one -0.989859 two 2.143608foo one 1.443110 two -1.413542qux one NaN two NaNdtype: float64In [35]: s + s[::2]Out[35]: bar one -1.723698 two NaNbaz one -0.989859 two NaNfoo one 1.443110 two NaNqux one -2.079150 two NaNdtype: float64
阅读全文
0 0
- Pandas MultiIndex(多重索引)
- pandas multiIndex
- pandas入门——多重索引
- python/pandas dataframe中multiindex的操作
- python/pandas/numpy数据分析(七)-MultiIndex
- [每日一答] [20151012] pandas中,groupby方法返回MultiIndex转成DataFrame
- pandas数据索引变换
- pandas索引对象
- Pandas 列索引操作
- pandas层次化索引
- pandas索引和选择
- pandas索引和选择
- Pandas-索引index
- Pandas索引方法介绍
- pandas通过ix 索引
- pandas层次化索引
- python pandas 序列索引
- python pandas 位置索引
- 54. Spiral Matrix
- 卫星地图数据在公路管理规划的应用
- 【HDU3045】【斜率优化DP】Picnic Cows题解
- 同余方程
- maven项目加载时候,install部分包失败
- Pandas MultiIndex(多重索引)
- arduino为何流行?
- 基于spring的Junit4测试
- 安装confluence
- PHP之小疑惑
- POJ
- 让你的代码量减少3倍!使用kotlin开发Android(一)
- 【effective java读书笔记】泛型(二)
- MAVEN常用插件解析