pandas中的索引index操作总结

前言

pandas与numpy的最大区别就是索引,pandas中索引是显式的,通过索引可以实现各种操作。

pandas中索引

Pandas中索引属性

对DataFrame:

df对应的列和行index属性:

df.columns

df.index

Pandas中索引基础

Pandas中的索引

轴标记的作用:

注意事项:

对Series而言,Series[label]返回的是对应label的单个值,Series[slicing]返回的是对应slicing的切片子Series;

对DataFrame而言,DataFrame[label]返回的是对应label的列的Series,DataFrame[slicing]返回的是对应行slicing的切片子DataFrame

DataFrame.loc[slicing]与DataFrame.iloc[slicing]的区别

  • DataFrame.loc[slicing]:

When slicing, thestart bound is included, AND the stop bound is included

  • DataFrame.iloc[slicing]:

When slicing, the start bounds is included, while theupper bound is excluded

Appendingoperation:

The.loc/[] operations can perform enlargement when setting a non-existant key forthat axis.

索引注意事项

type(df[“label”])        # pd.Series()
type(df[[“label”]])        # pd.DataFrame()

label和position混用问题

不能一个轴使用label,另一个轴使用position

pandas中的索引基础

将列变成索引或将索引变成列

列变成行索引

  • df=df.set_index('date')

  • df.index=df.iloc[:,n]

行变成列索引

  • df.columns = df.iloc[n,:]

  • df = df.rename(index={}, columns={})

行索引变成列

  • df['index'] = df.index

  • df.reset_index(level=0, inplace=True)

  • df.reset_index(level=['tick', 'obs'])

  • df['si_name'] =df.index.get_level_values('si_name') #si_name is the name of the subindex

Series转DataFrame

series.to_frame()

只是将series对象转化为dataframe对象

pandas中index操作(重要)

  1. df.set_index()

  1. df.reset_index()

  1. df.reindex()

改变索引值(index或columns)

  1. 直接修改属性

df.columns=[]

df.index=[]

  1. 使用rename或rename_axis函数

修改pandas中index或columns的名字

直接修改

使用rename方法

pandas中查看Series和DataFrame的索引值

pandas中将pandas.index转化为其他类型

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wugou2014

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值