Python pandas使用

       Pandas是Python的一个数据分析包,该工具为解决数据分析任务而创建,纳入大量库和标准数据模型,提供高效的操作数据集所需的工具,提供大量能使我们快速便捷地处理数据的函数和方法,是字典形式,基于NumPy创建,让NumPy为中心的应用变得更加简单。

一、pandas安装

pip install pandas

 

或者

conda install pandas

二、引用、导入

import pandas as pd

 

三、Pandas数据结构

3.1 Series

可以通过传递一个list对象来创建一个Series,pandas会默认创建整型索引

import numpy as np
import pandas as pd
s=pd.Series([1,2,3,np.nan,5,6])
print(s)#索引在左边 值在右边

结果为:

0    1.0
1    2.0
2    3.0
3    NaN
4    5.0
5    6.0
dtype: float64

可能出现的错误

AttributeError: module 'pandas' has no attribute 'Series'

解决方案

参考链接:https://blog.csdn.net/qq_27466325/article/details/81568501

最有可能出现的原因:安装的包没有成功,或者python文件命名问题。

3.2 DataFrame

通过传递一个numpyarray,时间索引以及列标签来创建一个DataFrame:

DataFrame是表格型数据结构,包含一组有序的列,每列可以是不同的值类型。DataFrame有行索引和列索引,可以看成由Series组成的字典。

dates=pd.date_range('20200428',periods=6)
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=['A','B','C','D'])#生成6行4列位置
print(df)#输出6行4列的表格

结果为:

                   A         B         C         D
2020-04-28 -2.136035 -1.024890  1.205626 -0.288044
2020-04-29 -0.322414  0.168437 -0.381742  1.451043
2020-04-30 -0.861941 -1.196570  0.854113  0.323263
2020-05-01  0.339969 -0.707309  0.145176  0.337080
2020-05-02 -1.598901 -1.938175  1.783464  0.646485
2020-05-03  0.310627  0.195648 -0.765011  0.936771
print(df['B'])

结果为:

2020-04-28   -0.189951
2020-04-29   -0.401457
2020-04-30   -1.614889
2020-05-01   -0.541527
2020-05-02   -0.423574
2020-05-03   -1.160125
Freq: D, Name: B, dtype: float64
#创建特定数据的DataFrame
df_1=pd.DataFrame({'A' : 1.,
                    'B' : pd.Timestamp('20180310'),
                    'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
                    'D' : np.array([3] * 4,dtype='int32'),
                    'E' : pd.Categorical(["test","train","test","train"]),
                    'F' : 'foo'
                    })
print(df_1)
print(df_1.dtypes)

结果为:

3  1.0 2018-03-10  1.0  3  train  foo
A           float64
B    datetime64[ns]
C           float32
D             int32
E          category
F            object
dtype: object

3.3 Pandas选择数据

dates=pd.date_range('20180310',periods=6)
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=['A','B','C','D'])#生成6行4列位置
print(df)
print(df['A'])#或者df.A 选择某列

结果为:

                   A         B         C         D
2018-03-10  0.477457 -0.932087 -1.057655  0.937636
2018-03-11  1.146670  0.713825  0.100919  0.889144
2018-03-12 -0.285888  0.884547 -0.038438 -0.009993
2018-03-13 -1.177240  0.439073 -0.380612 -0.601506
2018-03-14  0.321225 -1.001314  0.576907  0.215429
2018-03-15  1.289708 -0.189629  0.732746  0.085593
2018-03-10    0.477457
2018-03-11    1.146670
2018-03-12   -0.285888
2018-03-13   -1.177240
2018-03-14    0.321225
2018-03-15    1.289708
Freq: D, Name: A, dtype: float64

3.4 Pandas设置数据

根据loc和iloc设置

dates = pd.date_range('20180310', periods=6)
df = pd.DataFrame(np.arange(24).reshape((6,4)), index=dates, columns=['A', 'B', 'C', 'D'])
print(df)

df.iloc[2,2] = 999#单点设置
df.loc['2018-03-13', 'D'] = 999
print(df)

结果:

             A   B   C   D
2018-03-10   0   1   2   3
2018-03-11   4   5   6   7
2018-03-12   8   9  10  11
2018-03-13  12  13  14  15
2018-03-14  16  17  18  19
2018-03-15  20  21  22  23
             A   B    C    D
2018-03-10   0   1    2    3
2018-03-11   4   5    6    7
2018-03-12   8   9  999   11
2018-03-13  12  13   14  999
2018-03-14  16  17   18   19
2018-03-15  20  21   22   23

 

参考博客:https://www.jianshu.com/p/218baa41bab9

参考博客:https://www.jb51.net/article/163606.htm

参考博客:https://www.cnblogs.com/wrxblog/p/9752406.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章