统计
import pandasimport numpy# 通过传递一个 numpyarray,时间索引以及列标签来创建一个DataFrame:dates = pandas.date_range("20180509", periods=6)df = pandas.DataFrame(numpy.random.randn(6, 4), index=dates, columns=list('ABCD'))print("时间索引以及列标签来创建一个DataFrame:", df, sep="\n")# 描述性统计,求每一列的平均数print("每一列的平均数", df.mean(), sep="\n")# 其他轴的形同操作print("每一行的平均数", df.mean(1), sep="\n")# 对于拥有不同维度、需要对其的对象进行操作。Pandas会自动沿着指定的维度进行广播s = pandas.Series([1, 3, 5, numpy.nan, 6, 8], index=dates).shift(2) # shift函数主要的功能就是使数据框中的数据移动。print("行索引不变,移动列的数据。", s, sep="\n")print("df-s", df.sub(s, axis='index'), sep="\n")# 按照index进行匹配,为s补全为一个矩阵后进行计算,完成两个矩阵相减(df-s)
"E:\Python 3.6.2\python.exe" F:/PycharmProjects/test.py时间索引以及列标签来创建一个DataFrame: A B C D2018-05-09 0.689544 0.875232 0.452993 1.8756282018-05-10 -0.216719 0.298931 -1.159366 0.1889062018-05-11 0.268589 1.206928 -0.119726 -0.1487642018-05-12 -1.035244 1.092390 1.006421 -0.2261862018-05-13 0.670916 0.738597 -0.184312 -1.2808672018-05-14 -0.359534 1.109787 0.650537 -0.030985每一列的平均数A 0.002925B 0.886978C 0.107758D 0.062955dtype: float64每一行的平均数2018-05-09 0.9733492018-05-10 -0.2220622018-05-11 0.3017572018-05-12 0.2093452018-05-13 -0.0139172018-05-14 0.342451Freq: D, dtype: float64行索引不变,移动列的数据。2018-05-09 NaN2018-05-10 NaN2018-05-11 1.02018-05-12 3.02018-05-13 5.02018-05-14 NaNFreq: D, dtype: float64 A B C D2018-05-09 NaN NaN NaN NaN2018-05-10 NaN NaN NaN NaN2018-05-11 -0.731411 0.206928 -1.119726 -1.1487642018-05-12 -4.035244 -1.907610 -1.993579 -3.2261862018-05-13 -4.329084 -4.261403 -5.184312 -6.2808672018-05-14 NaN NaN NaN NaNProcess finished with exit code 0
函数apply()
import pandasimport numpy# 通过传递一个 numpyarray,时间索引以及列标签来创建一个DataFrame:dates = pandas.date_range("20180509", periods=6)df = pandas.DataFrame(numpy.random.randn(6, 4), index=dates, columns=list('ABCD'))print("时间索引以及列标签来创建一个DataFrame:", df, sep="\n")# 对数据应用函数print("从第一行开始,其下一行网上一行结果上累加:", df.apply(numpy.cumsum), sep="\n") # 每行数值向上求和print("每列的最大数减去最小数:", df.apply(lambda x: x.max() - x.min()), sep="\n")
"E:\Python 3.6.2\python.exe" F:/PycharmProjects/test.py时间索引以及列标签来创建一个DataFrame: A B C D2018-05-09 0.628765 -1.453298 -0.169228 -0.1850652018-05-10 0.444467 0.159900 -1.581807 0.8520652018-05-11 1.537534 -1.718371 -1.378338 -0.1839292018-05-12 -2.131473 -2.586691 -0.241944 -0.8424462018-05-13 -0.898688 0.394125 1.413996 -1.8975692018-05-14 -0.891981 0.913925 0.686605 -0.842980从第一行开始,其下一行网上一行结果上累加: A B C D2018-05-09 0.628765 -1.453298 -0.169228 -0.1850652018-05-10 1.073232 -1.293399 -1.751035 0.6670002018-05-11 2.610767 -3.011770 -3.129372 0.4830712018-05-12 0.479293 -5.598461 -3.371316 -0.3593742018-05-13 -0.419395 -5.204337 -1.957321 -2.2569442018-05-14 -1.311376 -4.290412 -1.270715 -3.099924每列的最大数减去最小数:A 3.669008B 3.500616C 2.995802D 2.749634dtype: float64Process finished with exit code 0
直方图
import pandasimport numpy# 通过传递一个 numpyarray,时间索引以及列标签来创建一个DataFrame:dates = pandas.date_range("20180509", periods=6)df = pandas.DataFrame(numpy.random.randn(6, 4), index=dates, columns=list('ABCD'))print("时间索引以及列标签来创建一个DataFrame:", df, sep="\n")s = pandas.Series(numpy.random.randint(0, 7, size=10))print("随机生成十个数的序列:", s, sep="\n")print("统计每个数出现的次数:", s.value_counts(), sep="\n")
"E:\Python 3.6.2\python.exe" F:/PycharmProjects/test.py时间索引以及列标签来创建一个DataFrame: A B C D2018-05-09 -1.447060 0.998378 -0.272173 -0.2408732018-05-10 2.019563 0.397001 1.469093 -0.3132722018-05-11 0.932445 0.973830 -1.914278 -1.3747482018-05-12 -0.980636 1.336340 -0.232319 1.1768332018-05-13 -1.850315 -0.738035 -1.085791 1.3788752018-05-14 1.162965 1.892369 0.499482 0.6474240 51 22 13 44 15 56 07 18 09 3dtype: int32Process finished with exit code 0
字符串方法
Series对象在其str属性中配备了一组字符串处理方法,可以很容易的应用到数组中的每个元素。
import pandasimport numpys = pandas.Series(['A', 'B', 'C', 'Aaba', 'Baca', numpy.nan, 'CABA', 'dog', 'cat'])print("序列值全部改成小写:", s.str.lower(), sep="\n")
"E:\Python 3.6.2\python.exe" F:/PycharmProjects/test.py序列值全部改成小写:0 a1 b2 c3 aaba4 baca5 NaN6 caba7 dog8 catdtype: objectProcess finished with exit code 0