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 pandas exports Excel tables

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces pandas how to export Excel table, the article is very detailed, has a certain reference value, interested friends must read it!

If we have such a data source, what happens when we import python for data processing without any processing?

Note: the serial number and ID card column are already text types in Excel.

What would happen if there was no processing for the read and no processing for the export?

Import pandas as pd

Df = pd.read_excel ("info.xlsx")

Df

The results are as follows:

This is super awkward! If there is a column here in my data source, isn't it speechless that you ran it once in python and made my original data display abnormally?

This is definitely not going to work. How should we solve this kind of problem?

First of all, let's take a look at how to read such data into python and how to display it normally.

Import pandas as pd

# converters parameter, you can specify to read certain columns and specify data types

Df = pd.read_excel ("info.xlsx", converters= {"ID card": str, "serial number": str})

Df

The results are as follows:

From the above figure, we can see that the data has been read normally. At this point, the data I want to export to excel does not have a format display problem, what should I do?

Df ["number"] = df ["number"] .apply (lambda x: "\ t" + x)

Df ["ID card"] = df ["ID card"] .apply (lambda x: "\ t" + x)

Df.to_csv ("info2.csv", encoding= "gbk")

Df.to_excel ("info2.xlsx", encoding= "gbk")

Pedal, you read it right. It's as simple as that. Just put a tab in front of each data. In fact, you can also put a single quotation mark in English in front of the data, but only after adding tabs and exporting excel, the result is the best.

Now let's look at the final result:

The above is all the contents of the article "how pandas exports Excel tables". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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

Internet Technology

Wechat

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

12
Report