简单概括精髓,pandas必知必会

大家好,我是jiejie,今天我们介绍pandas库当中一些非常基础的方法与函数,希望大家看了之后会有所收获!

准备需要的数据集

我们先准备生成一些随机数,作为后面需要用到的数据集

index = pd.date_range("1/1/2000", periods=8)

series = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"])

df = pd.DataFrame(np.random.randn(8, 3), index=index, columns=["A", "B", "C"])

Head and tail

head()和tail()方法是用来查看数据集当中的前几行和末尾几行的,默认是查看5行,当然读者朋友也可以自行设定行数

series2 = pd.Series(np.random.randn(100))
series2.head()

output

0    0.733801
1   -0.740149
2   -0.031863
3    2.515542
4    0.615291
dtype: float64

同理

series2.tail()

output

95   -0.526625
96   -0.234975
97    0.744299
98    0.434843
99   -0.609003
dtype: float64

数据的统计分析

在pandas当中用describe()方法来对表格中的数据做一个概括性的统计分析,例如

series2.describe()

output

count    100.000000
mean       0.040813
std        1.003012
min       -2.385316
25%       -0.627874
50%       -0.029732
75%        0.733579
max        2.515542
dtype: float64

当然,我们也可以设置好输出的分位

series2.describe(percentiles=[0.05, 0.25, 0.75, 0.95])

output

count    100.000000
mean       0.040813
std        1.003012
min       -2.385316
5%        -1.568183
25%       -0.627874
50%       -0.029732
75%        0.733579
95%        1.560211
max        2.515542
dtype: float64

对于离散型的数据来说,describe()方法给出的结果则会简洁很多

s = pd.Series(["a", "a", "b", "b", "a", "a", "d", "c", "d", "a"])
s.describe()

output

count     10
unique     4
top        a
freq       5
dtype: object

要是表格中既包含了离散型数据,也包含了连续型的数据,默认的话,describe()是会针对连续型数据进行统计分析

df2 = pd.DataFrame({"a": ["Yes", "Yes", "No", "No"], "b": np.random.randn(4)})
df2.describe()

output

              b
count  4.000000
mean   0.336053
std    1.398306
min   -1.229344
25%   -0.643614
50%    0.461329
75%    1.440995
max    1.650898

当然我们也可以指定让其强制统计分析离散型数据或者连续型数据

df2.describe(include=["object"])

output

          a
count     4
unique    2
top     Yes
freq      2

同理,我们也可以指定连续型的数据进行统计分析

df2.describe(include=["number"])

output

              b
count  4.000000
mean  -0.593695
std    0.686618
min   -1.538640
25%   -0.818440
50%   -0.459147
75%   -0.234401
max    0.082155

如果我们都要去做统计分析,可以这么来执行

df2.describe(include="all")

output

          a         b
count     4  4.000000
unique    2       NaN
top     Yes       NaN
freq      2       NaN
mean    NaN  0.292523
std     NaN  1.523908
min     NaN -1.906221
25%     NaN -0.113774
50%     NaN  0.789560
75%     NaN  1.195858
max     NaN  1.497193

最大/最小值的位置

idxmin()和idxmax()方法是用来查找表格当中最大/最小值的位置,返回的是值的索引

s1 = pd.Series(np.random.randn(5))
s1

output

s1.idxmin(), s1.idxmax()

output

(0, 3)

用在DataFrame上面的话,如下

df1 = pd.DataFrame(np.random.randn(5, 3), columns=["A", "B", "C"])
df1.idxmin(axis=0)

output

A    4
B    2
C    1
dtype: int64

同理,我们将axis参数改成1

df1.idxmin(axis=1)

output

0    C
1    C
2    C
3    B
4    A
dtype: object

value_counts()方法

pandas当中的value_counts()方法主要用于数据表的计数以及排序,用来查看表格当中,指定列有多少个不同的数据值并且计算不同值在该列当中出现的次数,先来看一个简单的例子

df = pd.DataFrame({'城市': ['北京', '广州', '上海', '上海', '杭州', '成都', '香港', '南京', '北京', '北京'],
                   '收入': [10000, 10000, 5500, 5500, 4000, 50000, 8000, 5000, 5200, 5600],
                   '年龄': [50, 43, 34, 40, 25, 25, 45, 32, 25, 25]})
df["城市"].value_counts()

output

北京    3
上海    2
广州    1
杭州    1
成都    1
香港    1
南京    1
Name: 城市, dtype: int64

可以看到北京出现了3次,上海出现了2次,并且默认采用的是降序来排列的,下面我们来看一下用升序的方式来排列一下收入这一列

df["收入"].value_counts(ascending=True)

output

4000     1
50000    1
8000     1
5000     1
5200     1
5600     1
10000    2
5500     2
Name: 收入, dtype: int64

同时里面也还可以利用参数normalize=True,来计算不同值的计数占比

df['年龄'].value_counts(ascending=True,normalize=True)

output

50    0.1
43    0.1
34    0.1
40    0.1
45    0.1
32    0.1
25    0.4
Name: 年龄, dtype: float64

如果你觉得这篇文章,对你有点用的话,记得不要忘记3连,你的肯定就将是我持续输出更多优质文章的最强动力!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值