python数据科学(八):pandas基础—— 丢弃、apply applymap、唯一性

丢弃

  • 丢弃行 axis=0
  • 丢弃列 axis=1
import pandas as pd
import numpy as np

## 丢弃部分数据
df = pd.DataFrame(np.random.randn(4, 6), index=list('ABCD'), columns=['one', 'two', 'three', 'four', 'five', 'six'])
print('丢弃')
print(df)
print(df.drop('A'))
df2 = df.drop(['two', 'four'], axis=1)
print(df2)

在这里插入图片描述

apply&appymap函数

  • apply: 将数据按行或列进行计算,可标量可序列
    默认按列操作 axis=0
    按行操作 axis=1
  • applymap: 将数据按元素为进行计算
import pandas as pd
import numpy as np

### 函数应用
df = pd.DataFrame(np.arange(12).reshape(4, 3), index=['one', 'two', 'three', 'four'], columns=list('ABC'))
print('函数应用')


# apply
##标量
# 每一列作为一个 Series 作为参数传递给 lambda 函数
df.apply(lambda x: x.max() - x.min())
print(df)
# 每一行作为一个 Series 作为参数传递给 lambda 函数
df.apply(lambda x: x.max() - x.min(), axis=1)
print(df)

##序列
def min_max(x):
    return pd.Series([x.min(), x.max()], index=['min', 'max'])
df.apply(min_max, axis=1)



# applymap: 逐元素运算
df = pd.DataFrame(np.random.randn(4, 3), index=['one', 'two', 'three', 'four'], columns=list('ABC'))
print('applymap函数')
##只显示小数点前两位
formater = '{0:.02f}'.format
print(df.applymap(formater))

在这里插入图片描述

数据唯一性及成员资格

适用于 Series

### 数据唯一性及成员资格
s = pd.Series(list('abbcdabacad'))
print('数据唯一性及成员资格')
print(s.unique())
print(s.value_counts())
inin=s.isin(['a', 'b', 'c'])
print(inin)

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值