Python学习:Pandas(一)

一、Series

A Series is a single column of data, with row labels for each observation.
Pandas refers to the row labels as the index of the Series.
在这里插入图片描述

1. 默认下标

import numpy as np
import pandas as pd
S = pd.Series([11,28,72, 3, 5, 8])
print(S)
print(S.index) # 默认从0开始
print(S.values)

在这里插入图片描述

2. 自定义下标

fruits = ['apples','oranges','cherries','pears']
quantities = [20,33,52,19]
S = pd.Series(data = quantities,index = fruits)
print(S)

在这里插入图片描述

values = [5.6, 5.3, 4.3, 4.2, 5.8, 5.3, 4.6, 7.8, 9.1, 8., 5.7]
years = list(range(1995, 2017, 2))
unemp = pd.Series(data = values, index = years, name = 'Unemployment')

在这里插入图片描述
We can look at the index and values in our Series.

unemp.index
unemp.values

在这里插入图片描述

3. 对Seires数据进行简易处理

查看头尾数据

unemp.head()
unemp.tail()

基础绘图:使用.plot()方法

unemp.plot()

在这里插入图片描述
查看不重复的数据

unemp.unique()

在这里插入图片描述

4. Indexing

Sometimes, we will want to select particular elements from a Series.

We can do this using .loc[index_items]; where index_items is an item from the index, or a list of items in the index.

unemp.loc[1995]
unemp.loc[[1995,2005,2015]]
unemp.loc[1995:2007]

5. 对Series进行算数操作

print((S+3)*4)
print(np.sin(x))
S.apply(np.sin)

在这里插入图片描述

# 使用apply方法、叠加lambda函数
S.apply(lambda x : x if x>50 else x+10)

在这里插入图片描述

6. 序列拼接 pd.concat

参数axis = 0 表示在下方合并

x = pd.Series([1, 3, 5, 7])
y = pd.Series(['male','female','male','female'])
pd.concat([x,y], axis = 0)

在这里插入图片描述
axis = 1表示纵向合并

pd.concat([x,y], axis = 1)

在这里插入图片描述

7. 通过字典生成Series

cities = {'London':861546,'Berlin':173529,'Baecelona':184202}
city_series = pd.Series(cities)
print(city_series)

在这里插入图片描述
NaN: not a number

my_cities = ['London','Paris','Berlin','Hamburg']
my_city_series = pd.Series(cities,index=my_cities)
print(my_city_series)

在这里插入图片描述

8. isnull()、notnull()判断是否缺失

print(my_city_series.isnull())
print(my_city_series.notnull())

在这里插入图片描述
在这里插入图片描述

9. 过滤缺失元素

注意:dropna()方法不影响原数组,而是生成新数组

print(my_city_series.dropna()) # 默认axis = 0, 即删除行
print(my_city_series.dropna(inplace = True)) #原地操作

在这里插入图片描述

10. 填充缺失值

my_cities = ['London','Paris','Berlin','Hamburg']
my_city_series = pd.Series(cities,index=my_cities)
print(my_city_series.fillna(0)) # 用数值填充

在这里插入图片描述

11. 更改列名

names = {"NorthEast": "NE",
         "MidWest": "MW",
         "South": "S",
         "West": "W"}
unemp_region.rename(columns=names)

12. pd.read_excel()/ read_csv() 参数

(1)sheet_name = ’ '指定读取sheet
(2)默认header = 0, 默认读取第一行为表头;header = None, 不使用数据源作为表头;
(3)index_col = None,默认索引为0的列作为行标签
(4)针对csv, parse_dates = [‘列名’] 自动将这些列转换为日期类型

13. pandas库好用的内置函数

(1)累计最大/最小/求和/乘积:cum(sum|min|max|prod)
(2)百分比变化:pct_change
(3)差分:diff()
(4)绝对值:abs

14. apply()方法

def standardize_data(x):
    """
    Changes the data in a Series to become mean 0 with standard deviation 1
    """
    mu = x.mean()
    std = x.std()

    return (x - mu)/std
   
std_unemp = unemp.apply(standardize_data)
std_unemp.head()

15. 花式索引

df.loc[(df["California"] < 5.1) & (df["Texas"].isin([4.6, 4.3]))]
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值