pandas reindex用法 补充

import numpy as np
import pandas as pd

np.random.seed(666)

s1 = pd.Series([1, 2, 3, 4], index=[‘A’, ‘B’, ‘C’, ‘D’])
s2 = pd.Series([‘A’,‘B’,‘C’], index=[1, 5, 10])
df1 = pd.DataFrame(np.random.rand(25).reshape([5,5]), index=[‘A’,‘B’,‘D’,‘E’,‘F’], columns=[‘c1’,‘c2’,‘c3’,‘c4’,‘c5’])
#################
#print(df1.reindex(index=[‘A’, ‘B’], columns=[‘c1’, ‘c2’])) # index columns 分别切片是可以的

print(df1.reindex(index=[‘A’,‘B’], columns=[‘c1’, ‘c2’, ‘c11’])) # 没有的,用Nan 填充

#print(df1.reindex(index=[‘A’,‘B’], columns=[‘c1’, ‘c2’,‘c11’], fill_value=600)) # 一般填充的是浮点数

#print(df1.reindex(index=[‘A’,‘B’,‘G’,‘H’,‘I’], columns=[‘c1’,‘c2’,‘c11’], method=‘ffill’))
#下面新增的 GHI 是以总数据集df1中的最后一个行索引的值为标准, 总数据df1 的最后一行为 c1 c2 列分别为 0.023236 0.727321
#所以在横向上全部填充为 0.023236 0.727321 # 在纵列上 C11列所对应的A B 行 的数值 和 c1 列所对应的 A B 行的数值相同

print(df1.reindex(columns=[‘c1’,‘c2’,‘c3’,‘c11’,‘c12’], method=‘ffill’)) # 这里没有inplace=True 这个参数

c12列的值 和 c1 列的值 是一样的

#对于一个Series来说,可以使用drop,来丢掉某些 index

#print(s1.drop(‘A’))
#dataframe drop(A) 直接去掉一行
print("---- axis 默认是行 ---- ")
print(df1.drop(‘A’, axis=0))

print("----- 将 c1 的列去掉----")
print(df1.drop(‘c1’, axis=1)) # 这个是去掉drop 中的列,剩下的数据集
#print(df1.drop(‘c1’, axis=1,inplace=True)) # 这里是可以加 inplace = True

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值