Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to convert a two-dimensional table into an one-dimensional table with python

Shulou Source: shulou.com Published: 2022-06-01 21:27:38 09月27日 Update

This article mainly explains "how to use python to turn a two-dimensional table into an one-dimensional table". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use python to turn a two-dimensional table into an one-dimensional table".

1. Read the table correctly

First, read the table in the traditional way:

Import pandas as pddata1 = pd.read_excel ('number of high school students .xlsx') data1

It is found that the index column is not recognized, resulting in a Unnamed: 0 column, so we should set the first column as the index column, the code is as follows:

Import pandas as pddata1 = pd.read_excel ('number of high school students .xlsx', index_col=0) # index_col is used to set the index column data1

In this way, the form can be read and recognized normally.

two。 Reset index

This step is mainly to reset the index column to a normal column, so as to facilitate the next step, the code is as follows

Data2=data1.reset_index () data2

As you can see, the previous index column programmed the 'index' column.

3. Convert column names to column data

This step mainly uses the melt function of pandas. Melt is a reverse operation function, which can convert column names into column data (columns name → column values) and reconstruct DataFrame. The usage is as follows:

Pandas.melt (frame, id_vars=None, value_vars=None, var_name=None, value_name='value', col_level=None)

Parameter explanation:

Frame: the dataset to be processed; id_vars: the column name that does not need to be converted; value_vars: the column name that needs to be converted, if all the remaining columns need to be converted, you do not need to write; var_name and value_name are the column names corresponding to the custom settings; col_level: use this level if the column is MultiIndex.

We keep the 'index' column and name the converted column' year',value 'stu_num':.

Data3=data2.melt (id_vars='index', var_name='year',value_name='stu_num') data3

4. Set the first column as the index column

To prevent the saved table from having a numeric index, you need to set the first column as the index column:

Data4=data3.set_index ('index') data4

5. Save table data4.to_excel ('converted form .xlsx')

When it's done, the above code can be done with 1 line of code:

Data=data.reset_index (). Melt ('index', var_name='col'). Set_index (' index') Thank you for your reading. The above is the content of "how to use python to turn a two-dimensional table into an one-dimensional table". After the study of this article, I believe you have a deeper understanding of how to use python to turn a two-dimensional table into an one-dimensional table. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Tables indexes one-dimensional two-dimensional code data learning content functions quantity high school high school students ordinary done no tradition parameters great work that is ideas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Huawei OPPO Reno Shulou Technology MariaDB