How to import xlsx using pd in python
Python uses pandas and xlsxwriter to read and write xlsx files. Here are the relevant steps:
The existing xlsx files are as follows:
1. Read all the data in the first n rows
# coding: utf-8
import pandas as pd
# Read all the data in the first n rows
df = pd.read_excel('school.xlsx')#Read the first sheet in xlsx
data1 = df .head(7) # Read all the data in the first 7 rows, dataFrame structure
data2 = df.values #list format, read all the data in the table
print("Get all Value:\n{0}".format(data1)) #Formatted output
print("Get all values:\n{0}".format(data2)) #Formatted output
2. Read specific rows and columns
# coding: utf-8
import pandas as pd
# Read Get specific rows and columns
df = pd.read_excel('school.xlsx') #Read the first sheet in xlsx
data1 = df.ix[0].values # Read all the data in the first row, 0 means the first row, excluding the header
data2 = df.ix[1,1] #Read the specified row and column position data
data3 = df .ix[[1,2]].values #Read specified multiple rows
data4 = df.ix[:,[0]].values #Read all rows of specified column
#data4 = df[u'class'].values #Same as above
data5 = df.ix[:,[u'class',u'name']].values #Read the specified key value column All lines
print("Data:\n{0}".format(data1))
print("Data:\n{0}".format(data2))
print("Data:\n{0}".format(data3))
print("Data:\n{0}".format(data4))
print("Data:\n{0}".format(data5))
Related recommendations: "Python Video Tutorial"
3. Get xlsx file line number, all column names
# coding: utf-8
import pandas as pd
# Get xlsx file line number, all column names
df = pd.read_excel('school.xlsx') #Read the first sheet in xlsx
print("Output line number list{}".format(df.index.values )) # Get all row numbers of the xlsx file
print("Output column title{}".format(df.columns.values)) #All column names
4. Read xlsx data and convert it into dictionary
# coding: utf-8
import pandas as pd
# Read xlsx data and convert it into dictionary
df = pd.read_excel('school.xlsx') #Read the first sheet in xlsx
test_data=[]
for i in df.index.values:#Get The index of the row number and traverse it:
#Get the data specified in each row based on i and use to_dict to convert it into a dictionary
row_data=df.ix[i,['id ','name','class','data','stature']].to_dict()
test_data.append(row_data)
print("The final data obtained is :{0}".format(test_data))
5. Write xlsx file
#coding: utf-8
import xlsxwriter
# Create workbook
file_name = "first_book.xlsx"
workbook = xlsxwriter.Workbook(file_name)
# Create worksheet
worksheet = workbook.add_worksheet('sheet1')
# Write cells
worksheet.write(0, 0, 'id')
worksheet.write(0 ,1, 'name')
worksheet.write(0,2, 'class')
worksheet.write(0,3, 'data')
# Write rows
worksheet.write_row(1, 0, [1, 2, 3])
# Write columns, where column D needs to be capitalized
worksheet.write_column(' D2', ['a', 'b', 'c'])
# Close the workbook
workbook.close()
The xlsx file written is as follows :
##
The above is the detailed content of How to import xlsx using pd in python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

Efficient training of PyTorch models on CentOS systems requires steps, and this article will provide detailed guides. 1. Environment preparation: Python and dependency installation: CentOS system usually preinstalls Python, but the version may be older. It is recommended to use yum or dnf to install Python 3 and upgrade pip: sudoyumupdatepython3 (or sudodnfupdatepython3), pip3install--upgradepip. CUDA and cuDNN (GPU acceleration): If you use NVIDIAGPU, you need to install CUDATool

Enable PyTorch GPU acceleration on CentOS system requires the installation of CUDA, cuDNN and GPU versions of PyTorch. The following steps will guide you through the process: CUDA and cuDNN installation determine CUDA version compatibility: Use the nvidia-smi command to view the CUDA version supported by your NVIDIA graphics card. For example, your MX450 graphics card may support CUDA11.1 or higher. Download and install CUDAToolkit: Visit the official website of NVIDIACUDAToolkit and download and install the corresponding version according to the highest CUDA version supported by your graphics card. Install cuDNN library:

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

When selecting a PyTorch version under CentOS, the following key factors need to be considered: 1. CUDA version compatibility GPU support: If you have NVIDIA GPU and want to utilize GPU acceleration, you need to choose PyTorch that supports the corresponding CUDA version. You can view the CUDA version supported by running the nvidia-smi command. CPU version: If you don't have a GPU or don't want to use a GPU, you can choose a CPU version of PyTorch. 2. Python version PyTorch

CentOS Installing Nginx requires following the following steps: Installing dependencies such as development tools, pcre-devel, and openssl-devel. Download the Nginx source code package, unzip it and compile and install it, and specify the installation path as /usr/local/nginx. Create Nginx users and user groups and set permissions. Modify the configuration file nginx.conf, and configure the listening port and domain name/IP address. Start the Nginx service. Common errors need to be paid attention to, such as dependency issues, port conflicts, and configuration file errors. Performance optimization needs to be adjusted according to the specific situation, such as turning on cache and adjusting the number of worker processes.

Efficiently process PyTorch data on CentOS system, the following steps are required: Dependency installation: First update the system and install Python3 and pip: sudoyumupdate-ysudoyuminstallpython3-ysudoyuminstallpython3-pip-y Then, download and install CUDAToolkit and cuDNN from the NVIDIA official website according to your CentOS version and GPU model. Virtual environment configuration (recommended): Use conda to create and activate a new virtual environment, for example: condacreate-n
