大家好,我是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连,你的必定就将是我继续输入更多优质文章的最强能源!