python pandas

1,pandas介绍

The name Pandas is derived from the econometrics term Panel Data. Pandas incorporates two additional data structures into Python, namely Pandas Series and Pandas DataFrame.

pandas是在numpy的基础上建立起来的,能够与numpy结合起来使用。

一个dataframe中容许有多种不同的数据类型。

2,pd.Series()

1,与numpy的array很相似,一个series中只能有一种数据类型。与numpy不同之处是多了index参数(numpy的函数操作对series也适用)。
python pandas
python pandas
2,可以通过loc和iloc访问数据
python pandas
3,删除数据(不加inplace=True就不会改变原来的groceries)python pandas
4,对于字符型变量,容许有乘以2的操作,即将字符串复制一遍(在numpy中不容许)。
python pandas

3,pd.DataFrame()

1,不同列容许是不同数据类型。

2,创建dataframe。
python pandas
3,index参数用来访问行,column参数用来访问列。

4,处理NaN数据
#去掉含有NaN的行或列
x.dropna(axis = 0)或x.dropna(axis = 1)

#将所有的NaN用0代替
x.fillna(0)

#将NaN用它前面一行或列的数代替
x.fillna(method = ‘ffill’, axis = 0)或x.fillna(method = ‘ffill’, axis = 1)

#将NaN用它后面一行或列的数代替
x.fillna(method = ‘backfill’, axis = 0)或x.fillna(method = ‘backfill’, axis = 1)

#将NaN用它同行或列的前后平均数代替
x.interpolate(method = ‘linear’, axis = 0)或x.interpolate(method = ‘linear’, axis = 1)