Python Pandas基础运算

重新修改索引

s = pd.Series([1, 3, 5, 6, 8], index=list('acefh'))
print(s.reindex(list('abcdefgh')))
a    1.0
b    NaN
c    3.0
d    NaN
e    5.0
f    6.0
g    NaN
h    8.0

把默认数字填充为0

print(s.reindex(list('abcdefgh'), fill_value=0))
a    1
b    0
c    3
d    0
e    5
f    6
g    0
h    8

把前面的数值填充到后面的默认值里,method方法只对行有效

print(s.reindex(list('abcdefgh'), method='ffill'))
a    1
b    1
c    3
d    3
e    5
f    6
g    6
h    8

method='bfill’是向上填充

每一列的最大值减去最小值

df = pd.DataFrame(np.arange(12).reshape(4, 3), index=['one', 'two', 'three', 'four'], columns=list('ABC'))
print(df)
print(df.apply(lambda x: x.max()-x.min()))
       A   B   C
one    0   1   2
two    3   4   5
three  6   7   8
four   9  10  11
A    9
B    9
C    9

每一行的最大值减最小值

print(df.apply(lambda x: x.max()-x.min(), axis=1))
one      2
two      2
three    2
four     2
def min_max(x):
    return pd.Series([x.min(), x.max()], index=['min', 'max'])
print(df.apply(min_max, axis=1))
   min  max
one      0    2
two      3    5
three    6    8
four     9   11

只显示小数点后三位

formater = lambda x: '%.03f' % x
print(df.applymap(formater))
           A       B       C
one    0.000   1.000   2.000
two    3.000   4.000   5.000
three  6.000   7.000   8.000
four   9.000  10.000  11.000

根据某一列进行排序

print(df.sort_values(by='one'))
 one  two  three
A    1    5      4
D    6    7      8
C    7    9      7
B    8    7      4

查看这一个元素大小,在某一列或某一行里排名第几

s = pd.Series([3, 6, 2, 6, 4])
0    3
1    6
2    2
3    6
4    4
print(s.rank(method='first'))

第0号位置的元素排第二位,第一号位置的元素排第4位

0    2.0
1    4.0
2    1.0
3    5.0
4    3.0

数据的唯一性
统计每个元素出现的次数

print(s.value_counts())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值