python工具包pandas,数据分析
1 import语句
1 | import pandas as pd |
2 文件读取
1 | df = pd.read_csv(path='file.csv') |
3 数据预处理
1 | df.duplicated() 返回各行是否是上一行的重复行 |
4 数据筛选
1 | df.columns 列名,返回Index类型的列的集合 |
5 数据运算与排序
1 | df.T DataFrame转置 |
6 数学统计
1 | sr.unique Series去重 |
1. 对象创建 Data Structure Intro section
1.1 Series Series
1 | In [4]: s = pd.Series([1,3,5,np.nan,6,8]) |
1.2 DataFrame DataFrame
1 | In [6]: dates = pd.date_range('20130101', periods=6) |
2. 导入数据
1 | pd.read_csv() 默认分割符为逗号 |
3. 查看数据Basics section
1 | df.head() 查看开始五行 |
4. 选择
Indexing and Selecting Data MultiIndex / Advanced Indexing
4.1 get
1 | df['A'] 得到一列,想当年于df.A |
4.2 通过标签选择 Selection by Label
1 | df.loc[dates[0]] 通过行标签获取横截面 |
4.3 通过位置选择Selection by Position
1 | df.iloc[3] 获得第三行横截面 |
4.4 布尔索引isin()
1 | df[df.A > 0] 获得A列中大于0的行 |
4.5 设置
1 | pd.Series([1,2,3,4,5,6], index=pd.date_range('20130102', periods=6)) 设置新列自动按索引排列数据 |
###
5. 数据缺失 Missing Data section
1 | df1 = df.reindex(index=dates[0:4], columns=list(df.columns) + ['E']) |
###
6. 操作Basic section on Binary Ops
6.1 统计
1 | df.mean() 描述性统计 |
6.2 Apply
1 | In [66]: df.apply(np.cumsum) |
6.3 直方图化 Histogramming and Discretization
1 | s = pd.Series(np.random.randint(0, 7, size=10)) |
6.4 字符串方法Vectorized String Methods.
1 | s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat']) |
7. MergeMerging section
7.1 Concat concat()
1 | df = pd.DataFrame(np.random.randn(10, 4)) |
7.2 Join Database style joining
1 | left = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [1, 2]}) |
7.3 AppendAppending
1 | df = pd.DataFrame(np.random.randn(8, 4), columns=['A','B','C','D']) |
###
8.GroupingGrouping section
1 | df.groupby('A').sum() 分组,然后将函数总和应用于结果组。 |
9.Reshape Reshaping
Hierarchical Indexing
9.1 堆stack()
1 | index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second']) |
9.2 数据透视表Pivot Tables
1 |