Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use pandas to read xlsx files in python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "python how to use pandas to read xlsx files" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "python how to use pandas to read xlsx files" article can help you solve the problem.

Use pandas to read xlsx files

Read the first n rows of data

Read specified data (specified row, specified column)

Get the file line number and column header

Convert data to dictionary form

Import pandas as pd#1. Read all data in the first n rows df1=pd.read_excel ('d1.xlsx') # read the first sheetdata1=df1.head in xlsx (10) # read all data in the first 10 rows data2=df1.values#list [] is equivalent to a matrix Report an error in the behavior unit # data2=df.values (): TypeError: 'numpy.ndarray' object is not callableprint ("get all values:\ n {0}" .format (data1)) # format output print ("get all values:\ n {0}" .format (data2)) # 2. Read specific row specific column data3=df1.iloc [0] .values # read all data in the first row data4=df1.iloc [1] # read specified row and column position data: read (1 1) data of location data5=df1.iloc [[1jin2]] .values # read specified multiple rows: read all data data6=df1.iloc of the first row and the second row [: [0]] .values # read all row data of the specified column: read all data in the first column print ("data:\ n {0}" .format (data3)) print ("data:\ n {0}" .format (data4)) print ("data:\ n {0}" .format (data5)) print ("data:\ n {0}" .format (data6)) # 3. Get the xlsx file line number, column number print ("output line number list {}" .format (df1.index.values)) # get the numbers of all lines: 0, 1, 2, 3, 4print ("output column header {}" .format (df1.columns.values)) # that is the first element # 4 of each column. Convert the xlsx data into a dictionary data= [] for i in df1.index.values:# to get the index of the line number and traverse it # to get the specified data of each row according to I And use to_dict to convert the dictionary row_data=df1.loc [I, ['id','name','class','data','score',]] .to _ dict () data.append (row_data) print ("the final data is: {0}" .format (data)) # iloc and loc: iloc is indexed by line number, loc is indexed by index. # so 1Jing 2 Jing 3 should use iloc,4 should have loc data: d1.xlsxidnameclassdatascore201901A1Jan-201.3201902B2Mar-203.4201903C3May-203.4201904D1Jan-203.4201905E1Feb-205.6201906F1Mar-204.6201907G1Feb-197.8201908H2Apr-305.6201909I3Jan-425.6201910G4Mar-304.5201911K5Apr-203.4201912L6Apr-202.3201913M4Mar-202.4 run result display

This is the end of the introduction to "how python uses pandas to read xlsx files". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report