In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-11-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains the "python two-dimensional table how to convert into one-dimensional table", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "python two-dimensional table how to convert into one-dimensional table" bar!
# load data import pandas as pddf_old1 = pd.read_excel (r "D:\ Jupyter\ data\ Python.xlsx", sheet_name = "change one dimension") df_old1
# data cleaning, set the first column to index column df_old2 = pd.read_excel (r "D:\ Jupyter\ data\ Python.xlsx", sheet_name = "change one dimension", index_col = 0) df_old2
# reset index df_old3 = df_old2.reset_index () df_old3
Convert column names to column data
Here we need to use the .melt () function of pandas. Melt () is a reverse operation function that converts column names into column data (columns_name → column_values) and reconstructs DataFrame as follows:
The data3=data2.melt (id_vars='index', var_name='year',value_name='stu_num') data3 parameter explains: DataFrame: the dataset to be processed; id_vars: the column name that does not need to be converted; value_vars: the column name to be converted, if all the remaining columns need to be converted; var_name and value_name are the column names corresponding to the custom settings; col_level: use this level if the column is MultiIndex.
The results are as follows:
# convert 2D to 1D DF _ new = df_old3.melt (id_vars = "index", var_name = "year", value_name = "sales") df_new
The last step is in place:
# one step import pandas as pddf_melt = pd.read_excel (r "D:\ Jupyter\ data\ Python.xlsx", sheet_name = "change one dimension", index_col = 0) .reset_index () .melt (id_vars = "index", var_name = "year", value_name = "sales") .rename (columns = {"index": "province"}) df_melt
Thank you for your reading, the above is the content of "how to convert a python two-dimensional table into an one-dimensional table". After the study of this article, I believe you have a deeper understanding of how to convert a two-dimensional python 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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.