当前位置:   article > 正文

Pandas高阶篇六(数据的过滤和筛选)_pandas中判断一个series值是否大于某值

pandas中判断一个series值是否大于某值
from pandas import Series,DataFrame
import pandas as pd
import numpy as np
from numpy import nan as NA 
from matplotlib import pyplot as plt
np.random.seed(12345)
data = np.random.randn(1000,4)  #1000行4列
df = DataFrame(data)  
df.describe()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

打印结果:

#获取数据的第四列
col = df[3]
#筛选出第四列中绝对值>3的值
col[np.abs(col)>3]
#打印结果:
97     3.927528
305   -3.399312
400   -3.745356
Name: 3, dtype: float64

#查找出数据集中任意某一列的值出现大于3的行数据
df[(np.abs(df)>3).any(1)]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

打印结果:

#将所有绝对值大于3的整数设置3,绝对值大于3的负数,设置为-3
#获取数据的符号
df[np.abs(df)>3] = np.sign(df)*3
df   #原数据大于3的变成3,小于-3的变成-3
  • 1
  • 2
  • 3
  • 4
  • 5

打印结果:

#np.random.seed(number)  number是生成随机数的种子
# np.random.randn()  #默认生成随机数的种子数是当前时间的时间戳
#定义一个种子数,种子数只能用一次
np.random.seed(34567)
np.random.rand()
#打印结果:
0.542289024619481

#种子数一样,产生的随机数也一样
np.random.seed(34567)
np.random.rand()

#打印结果:
0.542289024619481
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/208299
推荐阅读
相关标签
  

闽ICP备14008679号