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 for python to read and write excel data

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about the detailed tutorials on python reading and writing excel data-pandas, which may not be understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Read and write excel data

1.1 read:

1.2 write:

Second, give examples

2.1 requirements

2.2 implementation

Summary

Read and write excel data

Using pandas, you can easily read and write excel data.

1. 1 read: data_in = pd.read_excel ('M2FENZISHI.xlsx') 1.2 write:

The first step is to create a data box

# exampledf = pd.DataFrame ({'Aguilar: [0meme 1mae2]}) writer = pd.ExcelWriter (' test.xlsx') # name of excel filedf.to_excel (writer, sheet_name='Sheet1') # writewriter.save () # save II, example 2.1 requirements

This example is a little more complicated, just look at the reading and writing parts.

The goal of the example is to have an excel file, as follows:

Now separate the numbers from the letters in the chemical symbols and get the following results

2.2 implementation

Because there are numbers and letters in chemical symbols, the first thing that comes to mind to extract numbers or letters is the regular expression re module.

Since we have named the first column data at read time, pandas can read only the nominations for this column directly.

You can use re.compile to read numbers, such as:

Here is the code for the complete implementation

Import numpy as np import re import pandas as pddata_in = pd.read_excel ('M2FENZISHI.xlsx') [' data'] # load dataprint (data_in.shape) length = len (data_in) # lengthpattern = re.compile (r'\ dbath') # find numbernum_out = [] for i in range (length): temp = pattern.findall (data_ in [I]) # find number int_num = list (map (int) Temp)) num_out.append (int_num) num_out = np.array (num_out) print (num_out.shape) # writer data to exceldf = pd.DataFrame ({'compressed: num_out [:, 0],' blocked: num_out [:, 1], 'obliterated: num_out [:, 2],' ordered: num_out [:, 3], 'pressed: num_out [:, 4] 'Saving: num_out [:, 5]}) writer = pd.ExcelWriter ('test.xlsx') # name of the filedf.to_excel (writer, sheet_name='Sheet1') writer.save ()

The results are as follows:

After summarizing the above, do you have any further understanding of python's detailed tutorial on reading and writing excel data-pandas? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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: 214

*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