当前位置: 首页 > news >正文

聊城做网站多少钱baidu com百度一下

聊城做网站多少钱,baidu com百度一下,杭州旅游 网站建设,小程序的功能与作用1.读excel 1.to_dict() 函数基本语法 DataFrame.to_dict (self, orientdict , into ) --- 官方文档 函数种只需要填写一个参数:orient 即可 ,但对于写入orient的不同,字典的构造方式也不同,官网一共给出了6种&#xff0c…

1.读excel

1.to_dict() 函数基本语法

DataFrame.to_dict   (self,   orient='dict'  ,   into=  ) --- 官方文档

函数种只需要填写一个参数:orient 即可 ,但对于写入orient的不同,字典的构造方式也不同,官网一共给出了6种,并且其中一种是列表类型:

  • orient ='dict',是函数默认的,转化后的字典形式:{column(列名) : {index(行名) : value(值) )}};
  • orient ='list' ,转化后的字典形式:{column(列名) :{[ values ](值)}};
  • orient ='series' ,转化后的字典形式:{column(列名) : Series (values) (值)};
  • orient ='split' ,转化后的字典形式:{'index' : [index],‘columns' :[columns],’data‘ : [values]};
  • orient ='records' ,转化后是 list形式:[{column(列名) : value(值)}......{column:value}];
  • orient ='index' ,转化后的字典形式:{index(值) : {column(列名) : value(值)}};

备注:

1,上面中 value 代表数据表中的值,column表示列名,index 表示行名,如下图所示:

2,{ }表示字典数据类型,字典中的数据是以 {key : value} 的形式显示,是键名和键值一一对应形成的。

2,关于6种构造方式进行代码实例

六种构造方式所处理 DataFrame 数据是统一的,如下:

 
  1. >>> import pandas as pd

  2. >>> df =pd.DataFrame({'col_1':[1,2],'col_2':[0.5,0.75]},index =['row1','row2'])

  3. >>> df

  4. col_1 col_2

  5. row1 1 0.50

  6. row2 2 0.75

2.1,orient ='dict' — {column(列名) : {index(行名) : value(值) )}}

to_dict('list') 时,构造好的字典形式:{第一列的列名:{第一行的行名:value值,第二行行名,value值},....};

 
  1. >>> df

  2. col_1 col_2

  3. row1 1 0.50

  4. row2 2 0.75

  5. >>> df.to_dict('dict')

  6. {'col_1': {'row1': 1, 'row2': 2}, 'col_2': {'row1': 0.5, 'row2': 0.75}}

orient = 'dict 可以很方面得到 在某一列对应的行名与各值之间的字典数据类型,例如在源数据上面我想得到在col_1这一列行名与各值之间的字典,直接在生成字典查询列名为col_1

 
  1. >>> df

  2. col_1 col_2

  3. row1 1 0.50

  4. row2 2 0.75

  5. >>> df.to_dict('dict')['col_1']

  6. {'row1': 1, 'row2': 2}

2.2,orient ='list' — {column(列名) :{[ values ](值)}};

生成字典中 key为各列名,value为各列对应值的列表

 
  1. >>> df

  2. col_1 col_2

  3. row1 1 0.50

  4. row2 2 0.75

  5. >>> df.to_dict('list')

  6. {'col_1': [1, 2], 'col_2': [0.5, 0.75]}

orient = 'list' 时,可以很方面得到 在某一列 各值所生成的列表集合,例如我想得到col_2 对应值得列表:

 
  1. >>> df

  2. col_1 col_2

  3. row1 1 0.50

  4. row2 2 0.75

  5. >>> df.to_dict('list')['col_2']

  6. [0.5, 0.75]

2.3,orient ='series' — {column(列名) : Series (values) (值)};

orient ='series' 与 orient = 'list' 唯一区别就是,这里的 value 是 Series数据类型,而前者为列表类型

 
  1. >>> df

  2. col_1 col_2

  3. row1 1 0.50

  4. row2 2 0.75

  5. >>> df.to_dict('series')

  6. {'col_1': row1 1

  7. row2 2

  8. Name: col_1, dtype: int64, 'col_2': row1 0.50

  9. row2 0.75

  10. Name: col_2, dtype: float64}

2.4,orient ='split' — {'index' : [index],‘columns' :[columns],’data‘ : [values]};

orient ='split' 得到三个键值对,列名、行名、值各一个,value统一都是列表形式;

 
  1. >>> df

  2. col_1 col_2

  3. row1 1 0.50

  4. row2 2 0.75

  5. >>> df.to_dict('split')

  6. {'index': ['row1', 'row2'], 'columns': ['col_1', 'col_2'], 'data': [[1, 0.5], [2, 0.75]]}

orient = 'split' 可以很方面得到 DataFrame数据表 中全部 列名或者行名 的列表形式,例如我想得到全部列名:

 
  1. >>> df

  2. col_1 col_2

  3. row1 1 0.50

  4. row2 2 0.75

  5. >>> df.to_dict('split')['columns']

  6. ['col_1', 'col_2']

2.5,orient ='records' — [{column:value(值)},{column:value}....{column:value}];

注意的是,orient ='records' 返回的数据类型不是 dict ; 而是list 列表形式,由全部列名与每一行的值形成一一对应的映射关系:

 
  1. >>> df

  2. col_1 col_2

  3. row1 1 0.50

  4. row2 2 0.75

  5. >>> df.to_dict('records')

  6. [{'col_1': 1, 'col_2': 0.5}, {'col_1': 2, 'col_2': 0.75}]

这个构造方式的好处就是,很容易得到 列名与某一行值形成得字典数据;例如我想要第2行{column:value}得数据:

 
  1. >>> df

  2. col_1 col_2

  3. row1 1 0.50

  4. row2 2 0.75

  5. >>> df.to_dict('records')[1]

  6. {'col_1': 2, 'col_2': 0.75}

2.6,orient ='index' — {index:{culumn:value}};

orient ='index'2.1用法刚好相反,求某一行中列名与值之间一一对应关系(查询效果与2.5相似):

 
  1. >>> df

  2. col_1 col_2

  3. row1 1 0.50

  4. row2 2 0.75

  5. >>> df.to_dict('index')

  6. {'row1': {'col_1': 1, 'col_2': 0.5}, 'row2': {'col_1': 2, 'col_2': 0.75}}

  7. #查询行名为 row2 列名与值一一对应字典数据类型

  8. >>> df.to_dict('index')['row2']

  9. {'col_1': 2, 'col_2': 0.75}

2.写excel

1.pd.DataFrame.from_records例子

数据可以作为结构化的 ndarray 提供:

>>> data = np.array([(3, 'a'), (2, 'b'), (1, 'c'), (0, 'd')],
...                 dtype=[('col_1', 'i4'), ('col_2', 'U1')])
>>> pd.DataFrame.from_records(data)col_1 col_2
0      3     a
1      2     b
2      1     c
3      0     d

数据可以作为字典列表提供:

>>> data = [{'col_1': 3, 'col_2': 'a'},
...         {'col_1': 2, 'col_2': 'b'},
...         {'col_1': 1, 'col_2': 'c'},
...         {'col_1': 0, 'col_2': 'd'}]
>>> pd.DataFrame.from_records(data)col_1 col_2
0      3     a
1      2     b
2      1     c
3      0     d

数据可以作为具有相应列的元组列表提供:

>>> data = [(3, 'a'), (2, 'b'), (1, 'c'), (0, 'd')]
>>> pd.DataFrame.from_records(data, columns=['col_1', 'col_2'])col_1 col_2
0      3     a
1      2     b
2      1     c
3      0     d

2.pd.DataFrame.from_dict例子

代码

# -*- coding: utf-8 -*-
import xlrd
import os
import pandas as pdclass ExcelReader:def __init__(self, config):"""filepath: strsheetnames: listheader_index : int"""self.path = config['filepath']self.sheetnames = config.get('sheetnames',0)header_index = config.get('header_index',0)self.data = {}if not self.sheetnames:data_xls = pd.read_excel(self.path, sheet_name=0, header=header_index, )data_xls.fillna("", inplace=True)self.data[0] = data_xls.to_dict('records')else:for name in self.sheetnames:#每次读取一个sheetname内容data_xls = pd.read_excel(self.path,sheet_name=name,header=header_index,)data_xls.fillna("",inplace=True)self.data[name] = data_xls.to_dict('records')class ExcelWriter:"""支持多写一个表格多个sheet"""def __init__(self,config):self.path = config['filepath'] # str 路径self.sheetnames = config.get('sheetnames')  # list  sheet nameif not self.sheetnames:self.sheetnames = []self.writer = pd.ExcelWriter(self.path)self.data = {}  #key --sheet_name  value -- sheet data: dict:for name in self.sheetnames:self.data[name] = {}def to_excel(self, sheet_name=None, startrow=0, index=False):if not sheet_name:for name in self.sheetnames:df = pd.DataFrame.from_records(self.data[name])df.to_excel(self.writer, sheet_name=name, startrow=startrow, index=index)else:df = pd.DataFrame.from_records(self.data[name])df.to_excel(self.writer, sheet_name=sheet_name, startrow=startrow, index=index)def write_row(self, sheet_name, row_data: dict):"""sheet_name: sheet_name  可以为不存在self.sheet_name中的值"""if sheet_name not in self.data:self.sheet_name.append(sheet_name)self.data[sheet_name] = {}for col in row_data:self.data[sheet_name][col] = [row_data[col]]returnif not self.data[sheet_name]:for col in row_data:self.data[sheet_name][col] = [row_data[col]]else:for col in self.data[sheet_name]:self.data[sheet_name][col].append(row_data.get(col,''))def save(self):"""保存并关闭"""self.to_excel() #数据写入excel对象内self.writer.save() #保存并关闭

参考:

pandas 读取excel、一次性写入多个sheet、原有文件追加sheet_pandas 写入多个sheet-CSDN博客


文章转载自:
http://boxwood.brjq.cn
http://occasionality.brjq.cn
http://irrepressibly.brjq.cn
http://ostensibly.brjq.cn
http://lst.brjq.cn
http://matchsafe.brjq.cn
http://ergonomist.brjq.cn
http://barebones.brjq.cn
http://changeless.brjq.cn
http://hel.brjq.cn
http://semiprofessional.brjq.cn
http://conflicting.brjq.cn
http://crossgrained.brjq.cn
http://barograph.brjq.cn
http://antihelix.brjq.cn
http://degear.brjq.cn
http://comprisal.brjq.cn
http://tappoon.brjq.cn
http://griskin.brjq.cn
http://realm.brjq.cn
http://fibrocystic.brjq.cn
http://cerated.brjq.cn
http://expansionary.brjq.cn
http://monocase.brjq.cn
http://conglobulate.brjq.cn
http://guipure.brjq.cn
http://mastic.brjq.cn
http://strix.brjq.cn
http://mantic.brjq.cn
http://ototoxic.brjq.cn
http://doyley.brjq.cn
http://coinhere.brjq.cn
http://goblet.brjq.cn
http://helicograph.brjq.cn
http://vegas.brjq.cn
http://det.brjq.cn
http://capitulate.brjq.cn
http://orwellism.brjq.cn
http://crisscross.brjq.cn
http://paedagogue.brjq.cn
http://settleable.brjq.cn
http://spending.brjq.cn
http://cymbiform.brjq.cn
http://faxes.brjq.cn
http://loculation.brjq.cn
http://philomena.brjq.cn
http://redistribution.brjq.cn
http://juicer.brjq.cn
http://request.brjq.cn
http://featherbed.brjq.cn
http://evadable.brjq.cn
http://draggletailed.brjq.cn
http://snagged.brjq.cn
http://procarp.brjq.cn
http://botanical.brjq.cn
http://microenvironment.brjq.cn
http://ampulla.brjq.cn
http://eucalypti.brjq.cn
http://saveable.brjq.cn
http://putrescine.brjq.cn
http://geisha.brjq.cn
http://hitch.brjq.cn
http://deaconship.brjq.cn
http://arizona.brjq.cn
http://concinnity.brjq.cn
http://capacitor.brjq.cn
http://cockspur.brjq.cn
http://cowl.brjq.cn
http://nimbostratus.brjq.cn
http://coadjacent.brjq.cn
http://proceed.brjq.cn
http://foretopman.brjq.cn
http://minicamera.brjq.cn
http://podunk.brjq.cn
http://fingo.brjq.cn
http://unlade.brjq.cn
http://leger.brjq.cn
http://duopsony.brjq.cn
http://demulcent.brjq.cn
http://batrachoid.brjq.cn
http://mastfed.brjq.cn
http://proletarianize.brjq.cn
http://stellate.brjq.cn
http://tapette.brjq.cn
http://twofold.brjq.cn
http://shunpiker.brjq.cn
http://aerostation.brjq.cn
http://eidetically.brjq.cn
http://nunchakus.brjq.cn
http://emasculative.brjq.cn
http://stormful.brjq.cn
http://coleta.brjq.cn
http://volubility.brjq.cn
http://kawasaki.brjq.cn
http://regularize.brjq.cn
http://unbelted.brjq.cn
http://inexpansible.brjq.cn
http://bariatrician.brjq.cn
http://registration.brjq.cn
http://protocol.brjq.cn
http://www.dt0577.cn/news/59598.html

相关文章:

  • 做网站的去哪找私活可以打广告的平台
  • 桂林网站制作公司互联网广告平台排名
  • 建筑行业最新资讯seo产品优化免费软件
  • 免费做淘宝客网站电子商务平台建设
  • 东港区网站制作雅思培训班价格一般多少
  • 哪个网站能在线做司考题目企业查询信息平台
  • 免费在线观看电视剧的网站成都推广系统
  • 专做海岛游的网站自己做的网站怎么推广
  • ios移动网站开发西安网站建设公司
  • 现在主流的网站开发语言发免费广告电话号码
  • 怎么在虚拟主机上发布网站查询网 网站查询
  • 网站怎么做滚动图片软件开发交易平台
  • 太仓新网站优化网店推广实训报告
  • 营销型网站架构师最佳磁力吧ciliba
  • 苏州做网站哪家公司好建站平台哪个好
  • wordpress无法加载css样式seo优化技术培训中心
  • 做网站排版全网营销推广靠谱吗
  • 海口做什么网站比较好模板建站的网站
  • 合浦住房和城乡规划建设局网站线上培训
  • php网站开发技术要点软件商店安装
  • 北京建站方案网站建设方案模板
  • 做301网站打不开官网建设
  • 京东云wordpress后台优化是什么意思
  • 石家庄网站开发山西seo推广
  • 快速做网站费用seo能干一辈子吗
  • 做网站可以把文字做成图片吗网络营销案例及分析
  • 福州网站推广dz论坛seo
  • 深圳建设网站公百度网页版进入
  • 网站建设的拓扑结构国内好的seo
  • 小白怎么做网站搬家教程电商营销策划方案范文