大家好,我是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.7338011   -0.7401492   -0.0318633    2.5155424    0.615291dtype: float64

同理

series2.tail()

output

95   -0.52662596   -0.23497597    0.74429998    0.43484399   -0.609003dtype: float64

数据的统计分析

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

series2.describe()

output

count    100.000000mean       0.040813std        1.003012min       -2.38531625%       -0.62787450%       -0.02973275%        0.733579max        2.515542dtype: float64

当然,咱们也能够设置好输入的分位

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

output

count    100.000000mean       0.040813std        1.003012min       -2.3853165%        -1.56818325%       -0.62787450%       -0.02973275%        0.73357995%        1.560211max        2.515542dtype: float64

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

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

output

count     10unique     4top        afreq       5dtype: object

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

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

output

              bcount  4.000000mean   0.336053std    1.398306min   -1.22934425%   -0.64361450%    0.46132975%    1.440995max    1.650898

当然咱们也能够指定让其强制统计分析离散型数据或者连续型数据

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

output

          acount     4unique    2top     Yesfreq      2

同理,咱们也能够指定连续型的数据进行统计分析

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

output

              bcount  4.000000mean  -0.593695std    0.686618min   -1.53864025%   -0.81844050%   -0.45914775%   -0.234401max    0.082155

如果咱们都要去做统计分析,能够这么来执行

df2.describe(include="all")

output

          a         bcount     4  4.000000unique    2       NaNtop     Yes       NaNfreq      2       NaNmean    NaN  0.292523std     NaN  1.523908min     NaN -1.90622125%     NaN -0.11377450%     NaN  0.78956075%     NaN  1.195858max     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    4B    2C    1dtype: int64

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

df1.idxmin(axis=1)

output

0    C1    C2    C3    B4    Adtype: 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南京    1Name: 城市, dtype: int64

能够看到北京呈现了3次,上海呈现了2次,并且默认采纳的是降序来排列的,上面咱们来看一下用升序的形式来排列一下支出这一列

df["支出"].value_counts(ascending=True)

output

4000     150000    18000     15000     15200     15600     110000    25500     2Name: 支出, dtype: int64

同时外面也还能够利用参数normalize=True,来计算不同值的计数占比

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

output

50    0.143    0.134    0.140    0.145    0.132    0.125    0.4Name: 年龄, dtype: float64

如果你感觉这篇文章,对你有点用的话,记得不要遗记3连,你的必定就将是我继续输入更多优质文章的最强能源!