学习笔记 Day 28 (pandas )

1 统计出911电话类型数量

import  pandas as pd
import numpy as np

df = pd.read_csv('./911.csv')

# print(df.head())
# print(df.info())

# print(df['title'])
# print(df['title'].str.split(':').tolist())
temp_list = df['title'].str.split(':').tolist() # 把元素按,分隔开,然后装入列表
type_list = list(set(i[0] for i in temp_list)) # 循环列表,去重得出所有的类型装进列表中
# print(type_list)

# 创建一个全为零的列表
pd_zeros = pd.DataFrame(np.zeros((df.shape[0],len(type_list))),columns=type_list)
# print(pd_zeros)

# 往里面填充1
for i in type_list:
    pd_zeros[i][df['title'].str.contains(i)] = 1# contains(i) 包含i的时候,返回True
print(pd_zeros)

print(pd_zeros.sum(axis=0))

使用分组聚合方式:

import  pandas as pd
import numpy as np

df = pd.read_csv('./911.csv')

# print(df.head())
# print(df.info())

# print(df['title'])
# print(df['title'].str.split(':').tolist())
temp_list = df['title'].str.split(':').tolist() # 把元素按,分隔开,然后装入列表
type_list = [i[0] for i in temp_list] # 循环列表,去重得出所有的类型装进列表中

df['type'] = pd.DataFrame(np.array(type_list).reshape((df.shape[0],1)))
print(df)

print(df.groupby(by='type').count()['title'])

生成一段时间范围:

学习笔记 Day 28 (pandas )_第1张图片

 常见频率缩写:

学习笔记 Day 28 (pandas )_第2张图片

 pandas重采样:

统计数据中不同月份电话次数和可视化展示:

import  pandas as pd
import numpy as np

df = pd.read_csv('./911.csv')

df['timeStamp'] = pd.to_datetime(df['timeStamp']) # 转为时间序列

df.set_index('timeStamp',inplace=True)

# print(df)

df_count = df.resample('M').count()['title']

# print(df_count)
import matplotlib.pyplot as plt

plt.figure(figsize=(20,8),dpi=80)

_x = df_count.index
_y = df_count.values

_x = [i.strftime("%Y-%m-%d") for i in _x]# 时间格式化

plt.plot(range(len(_x)),_y)

plt.xticks(range(len(_x)),_x,rotation=45)

plt.show()

 不同月份,不同类型:

import  pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_csv('./911.csv')

df['timeStamp'] = pd.to_datetime(df['timeStamp']) # 转为时间序列

# 增加列
temp_list = df['title'].str.split(':').tolist()
type_list = [i[0] for i in temp_list]
# print(type_list)
df['type'] = type_list
# print(df)

df.set_index('timeStamp',inplace=True)
#
# # print(df)
plt.figure(figsize=(20,8),dpi=80)
for type_name,type_data in df.groupby(by='type'):

    df_count = type_data.resample('M').count()['title']
#
# # print(df_count)




    _x = df_count.index
    _y = df_count.values

    _x = [i.strftime("%Y-%m-%d") for i in _x]# 时间格式化

    plt.plot(range(len(_x)),_y)

    plt.xticks(range(len(_x)),_x,rotation=45)

plt.show()

结果:

学习笔记 Day 28 (pandas )_第3张图片

Periodindex:

学习笔记 Day 28 (pandas )_第4张图片

就是把分开的字符串通过period index组合成pandas时间类型 

以pm2.5的数据为列:

import pandas as pd
import  matplotlib.pyplot as plt

df = pd.read_csv('./BeijingPM20100101_20151231.csv')

# print(df.head())

# 设置好时间索引
period = pd.PeriodIndex(year=df['year'],month=df['month'],day=df['day'],hour=df['hour'],freq='H')

# print(period)

df['datetime'] = period

df.set_index('datetime',inplace=True)

df = df.resample('7D').mean()

# print(df)

df['PM_US Post'].dropna()

data = df['PM_US Post']

# 可视化

_x = data.index
_x = [i.strftime('%Y-%m-%d') for i in _x]
_y = data.values

plt.figure(figsize=(20,8),dpi=80)

plt.xticks(range(0,len(_x),10),_x[::10],rotation=45)

plt.plot(range(len(_x)),_y)

plt.show()

你可能感兴趣的:(python,基础,python,pandas)