In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.