博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据分析-day06-pandas-dataFrame案例分析1(方法二):获取title字段中包含物流运输业名称作为分类,统计各个分类的条数,
阅读量:4288 次
发布时间:2019-05-27

本文共 1876 字,大约阅读时间需要 6 分钟。

# -*- coding: utf-8 -*-# @File    : pandas_dataframe_add_new_class_demo.py# @Date    :  2020-01-06 17:49# @Author  : adminimport pandas as pdfrom matplotlib import pyplot as pltimport numpy as  np;df=pd.read_csv("../../data/911.csv");df=df.head(10);print(df.head(5))#前5行的title列的数据#print(df[:5]["title"])###########################################################截取字符串,获取分类#################################jieque_list=df["title"].str.split(": ").tolist();catory_list=[m[0] for m in jieque_list]print(catory_list)r=np.array(catory_list)print(r)###########################################################在原矩阵中新加一列#################################df['category']=pd.DataFrame(np.array(catory_list).reshape(df.shape[0],1))print(df.head)###########################################################分类求条数#################################cate=df.groupby(by="category")["title"].count();print(cate)b=np.array([[2,4,1,5,6,1],[9,5,76,23,5,9]])print(b)

结果:

    lat        lng  ...                        addr  e

0  40.297876 -75.581294  ...      REINDEER CT & DEAD END  1
1  40.258061 -75.264680  ...  BRIAR PATH & WHITEMARSH LN  1
2  40.121182 -75.351975  ...                    HAWS AVE  1
3  40.116153 -75.343513  ...          AIRY ST & SWEDE ST  1
4  40.251492 -75.603350  ...    CHERRYWOOD CT & DEAD END  1

[5 rows x 9 columns]

['EMS', 'EMS', 'Fire', 'EMS', 'EMS', 'EMS', 'EMS', 'EMS', 'EMS', 'Traffic']
['EMS' 'EMS' 'Fire' 'EMS' 'EMS' 'EMS' 'EMS' 'EMS' 'EMS' 'Traffic']
<bound method NDFrame.head of          lat        lng  ...  e  category
0  40.297876 -75.581294  ...  1       EMS
1  40.258061 -75.264680  ...  1       EMS
2  40.121182 -75.351975  ...  1      Fire
3  40.116153 -75.343513  ...  1       EMS
4  40.251492 -75.603350  ...  1       EMS
5  40.253473 -75.283245  ...  1       EMS
6  40.182111 -75.127795  ...  1       EMS
7  40.217286 -75.405182  ...  1       EMS
8  40.289027 -75.399590  ...  1       EMS
9  40.102398 -75.291458  ...  1   Traffic

[10 rows x 10 columns]>
category
EMS        8
Fire       1
Traffic    1
Name: title, dtype: int64
[[ 2  4  1  5  6  1]
 [ 9  5 76 23  5  9]]
 

转载地址:http://mxtgi.baihongyu.com/

你可能感兴趣的文章
Angular 路由使用整理(一)
查看>>
git回到指定版本命令
查看>>
cordova-plugin-splashscreen设置启动页面和图标
查看>>
cordova-plugin-camera相机插件使用
查看>>
cordova-plugin-media音频播放和录制
查看>>
Visual Studio 2017使用Emmet风格编写Html--ZenCoding
查看>>
Visual Studio Code v1.21发布
查看>>
C# Newtonsoft.Json JObject移除属性,在序列化时忽略
查看>>
Git移除版本控制操作
查看>>
Http缓存机制(转)
查看>>
C# 本地时间格式,UTC时间格式,GMT时间格式处理
查看>>
Windows系统搭建GitServer--Bonobo Git Server
查看>>
Bootstrap3 datetimepicker控件之smalot的使用
查看>>
小程序Canvas隐藏问题处理
查看>>
小程序scroll-view组件使用简介(转)
查看>>
Visual Studio Code设置中文包/配置中文语言
查看>>
Git重置登录密码问题,Git-remote Incorrect username or password ( access token )
查看>>
C#时间点字符串转换为日期,当天时间点判断
查看>>
Visual Studio Code v1.28.2发布
查看>>
js计算时间差示例
查看>>