数据分析-day04-pandas-dataFrame中索引和复合索引的操作

#!usr/bin/env python
#-*- coding:utf-8 _*-
'''
@author:Administrator
@file: pandas_dataframe_index_demo.py
@time: 2020-01-05 上午 11:14
'''
import pandas as pd;
import numpy as np
df=pd.DataFrame(np.arange(12).reshape(3,4))
print(df)
print(df.index);
print("赋值索引:")
df.index=list("abc");
df.columns=list("wxyz")
print(df)
print("重新设置赋值索引:")

c=df.reindex(list("acd"));
print(c)
print(df);

print("指定某一列设置赋值索引:")
d=df.set_index("y");
print(d)
print("指定复合索引设置赋值索引:")
e=df.set_index(["y","x"]);
print(e)
print(e.index)

 0  1   2   3
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11
RangeIndex(start=0, stop=3, step=1)
赋值索引:
   w  x   y   z
a  0  1   2   3
b  4  5   6   7
c  8  9  10  11
重新设置赋值索引:
     w    x     y     z
a  0.0  1.0   2.0   3.0
c  8.0  9.0  10.0  11.0
d  NaN  NaN   NaN   NaN
   w  x   y   z
a  0  1   2   3
b  4  5   6   7
c  8  9  10  11
指定某一列设置赋值索引:
    w  x   z
y           
2   0  1   3
6   4  5   7
10  8  9  11
指定复合索引设置赋值索引:
      w   z
y  x       
2  1  0   3
6  5  4   7
10 9  8  11
MultiIndex([( 2, 1),
            ( 6, 5),
            (10, 9)],
           names=['y', 'x'])

Process finished with exit code 0
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值