Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How pandas exports Excel tables

Shulou Source: shulou.com Published: 2022-06-01 18:53:25 09月10日 Update

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!

Tags: Data result identity ID card table content tab situation data source article type question tabulation no original awkward this is not picture above value interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS Redmi Shulou Technology Huawei