Pandas determines the header row dynamically

songofhawk 2021-10-26 00:33:31
pandas determines header row dynamically

Pandas Yes, it is python language-written , Super easy to use data processing tools , It also provides a particularly convenient excel Read write function , You can read in one sentence excel All data in the file :

import pandas as pd
dataframe = pd.read_excel(io=file_path_name, header=1)

read_excel Method has many parameters , among header Indicates which line is excel The title line of the file ——Pandas Will use the value in the header row , treat as dataframe Column name in . The number of lines starts from 0 Start counting , for instance excel It's like this :

DJQQFunkyMix DJQQFunkyMix
full name Gender
Zhang Dazhu male
Wang Cuihua Woman

Then the above code will put the first 2 The line should be the title line , Read it out dataframe There are two columns , Namely " full name " and " Gender ", It can be used dataframe[' full name '], To get the data in the first column .

But sometimes , We don't know which line is the title line , and excel The file is quite large , For example, there are tens of thousands of lines ,read_excel Time spent at one time , Maybe up to tens of seconds . At this time, we have to read the data at one time , First determine the position of the title line .

Then look for the characteristics of the title line first , For example, we know that the first column is always “ full name ”, We can cycle through all the cells in the first column of the table , Until I find “ full name ” until , Then confirm the title line .

Just right Pandas The bottom layer uses xlrd To read excel, This library does not need to be installed separately , Direct reference , Just add the following lines of code :

import pandas as pd
import xlrd
workbook = xlrd.open_workbook(file_path_name) # Open the specified excel file
sheet = workbook.sheets()[0] # Read the specified sheet form
i = 1 # Suppose by default , The first 2 The row is the header row
for i in range(10):
value = sheet.cell(i, 0).value # Line number 、 Column number , from 0 Start
if value == ' full name ':
break
workbook.release_resources()
dataframe = pd.read_excel(io=file_path_name, header=i)

So no matter which line is the title line , Can be correctly parsed

版权声明
本文为[songofhawk]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/10/20211012222250621J.html

  1. django channels channel_layer.group_send 造成内存溢出
  2. Python布置了个感觉不大理解的题..
  3. Python a posé une question qui ne semblait pas très compréhensible.
  4. Python中yield返回生成器的详细方法
  5. Python函数中apply、map、applymap的区别
  6. Python字符串前加f、r、b、u的不同用法
  7. 5分钟教会你用Python采集CSDN的热榜
  8. 5分鐘教會你用Python采集CSDN的熱榜
  9. 5 minutes pour vous apprendre à utiliser Python pour collecter des listes chaudes de csdn
  10. Quick start of automation -- python (1) - [variables] - half an hour a day
  11. Python爬虫:给我一个链接,快手视频随便下载
  12. Python爬蟲:給我一個鏈接,快手視頻隨便下載
  13. 经验丰富程序员才知道的15种高级Python小技巧
  14. 經驗豐富程序員才知道的15種高級Python小技巧
  15. 15 conseils Python avancés que les programmeurs expérimentés connaissent
  16. Python crawler: Donnez - moi un lien pour télécharger des vidéos rapides
  17. Python爬虫:给我一个链接,快手视频随便下载
  18. [algorithm learning] sword finger offer 64. Find 1 + 2 +... + n (Java / C / C + + / Python / go / trust)
  19. 怎么系统的学习python,有没有一些比较完整的资料,基础知识+框架+项目实战此类pdf
  20. Python crawler: Donnez - moi un lien pour télécharger des vidéos rapides
  21. Python project management and construction, these four tools are enough!
  22. IDE的使用,pycharm引入Python库
  23. In the 120 series columns, you can learn the python beautiful oup4 module, 7000 word blog + climb the ninth workshop network
  24. Django运行xadmin 报错解析 ImportError: cannot import name 'DEFAULT_FORMATS' from 'import_export.admin'
  25. Python程序大学课程写程序
  26. Programme Python Programme d'études collégiales
  27. Python程序大學課程寫程序
  28. Django runxadmin Error resolution importerror: cannot Import name 'default Formats' from 'import _ Export.admin»
  29. Python 函数式编程,看这一篇足够了!
  30. 太棒了!11个好用到起飞的「Python字典」知识点!
  31. 一道Python题目,求解答!
  32. 一道Python題目,求解答!
  33. Un problème Python, s'il vous plaît!
  34. C'est génial! 11 points de connaissance du dictionnaire Python pour le décollage!
  35. Python Functional Programming, This is enough!
  36. 在python中beta分布的问题?
  37. 一个python习题,没有什么头绪,是关于进制的转换和绘制的,想了几天了,不仅仅是2,8,16这种常见的进制转换
  38. Un exercice Python, qui n'a pas beaucoup d'idées, est sur la conversion et le rendu décimaux et a pensé pendant quelques jours, pas seulement 2, 8, 16 cette conversion décimale commune
  39. Un problème avec la distribution bêta en python?
  40. python实现简单的读取excel 内容,报错
  41. L'implémentation Python lit simplement le contenu d'Excel et signale les erreurs
  42. 用Python定义一个函数,接收n个数字,求这些参数数字的和
  43. Définissez une fonction en python, recevez n nombres et additionnez ces nombres de paramètres
  44. 上电Python写文件后,再断电后导致文件内容丢失
  45. 上電Python寫文件後,再斷電後導致文件內容丟失
  46. Une fois que Python est allumé pour écrire des fichiers, le contenu des fichiers est perdu après une panne de courant
  47. python套接字编程报错:ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
  48. 【Python从入门到精通】(二)怎么运行Python呢?有哪些好的开发工具(PyCharm)
  49. 【Python从入门到精通】(二)怎么运行Python呢?有哪些好的开发工具(PyCharm)
  50. Python语法1
  51. 2018年度最受推荐的10本Python书籍(初学者必看)
  52. Les 10 livres Python les plus recommandés en 2018 (obligatoire pour les débutants)
  53. Syntaxe Python 1
  54. Python语法1
  55. 在python 运行celery时候 AttributeError: 'NoneType' object has no attribute 'Redis'错误
  56. Attributeerror: 'nonetype' Object has no attribute 'redis' Error when Celery is running in Python
  57. Syntaxe Python 1
  58. Python celery is a plug-in that focuses on distributed asynchronous task processing and task scheduling!
  59. Python celery is a plug-in that focuses on distributed asynchronous task processing and task scheduling!
  60. 在python,使用scrapy爬虫框架