chatgpt enables Python-python_melt function

Python Melt function: a tool for data reshaping

In the process of data analysis, it is often encountered that the data needs to be reshaped from a wide table to a long table or vice versa. The melt function in Python provides a simple and fast way to solve this problem.

What is the Melt function?

The Melt function is a function in the pandas library that can reshape wide format data in a DataFrame into long format. Specifically, it is to convert some columns into rows to facilitate data analysis and visualization. The function is defined as follows:

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

The meanings of the parameters are as follows:

  • id_vars: Columns that need to be reserved, that is, columns that do not need to be converted into rows, which can be column names or column indexes.
  • value_vars: The columns that need to be converted into rows, which can be column names or column indexes.
  • var_name: The name of the column that is converted into a row, which can be customized.
  • value_name: The name of the new column, which can be customized.
  • col_level: When multi-layer column indexing, which level is the specific operation.

How to use the Melt function?

Below we use a simple example to demonstrate the use of the Melt function. First, we construct a DataFrame containing course grades.

import pandas as pd

data = {<!-- -->
    'Name': ['Xiaoming', 'Xiaogang', 'Xiaohong'],
    'Chinese score': [86, 92, 88],
    'Math score': [90, 89, 92],
    'English score': [91, 86, 90]
}

df = pd. DataFrame(data)
print(df)

The output is as follows:

 Name Chinese score Math score English score
0 Xiao Ming 86 90 91
1 Xiaogang 92 89 86
2 Little Red 88 92 90

Next, we use the melt function to convert the DataFrame into a long table.

df_melt = df.melt(id_vars=['name'], value_vars=['Chinese score', 'math score', 'English score'], var_name='subject\ ', value_name='grade')
print(df_melt)

The output is as follows:

 Name Subject Grade
0 Xiao Ming Chinese score 86
1 Xiaogang Chinese score 92
2 Xiaohong Chinese score 88
3 Xiao Ming's math score is 90
4 Xiaogang Mathematics score 89
5 Xiaohong Math score 92
6 Xiao Ming's English score is 91
7 Xiaogang English score 86
8 Xiaohong English score 90

It can be seen from the output results that the melt function reshaped the original DataFrame according to the name and subject, and the grade column was converted into rows.

Main functions realized by Melt function

In addition to converting wide format data into long format, the Melt function also has the following main functions.

1. Remove duplicate values

In the process of converting the data format, the problem of duplicate values is often encountered. At this time, the drop_duplicates parameter of the Melt function can be used to remove duplicate values.

df_melt_drop_dup = df_melt.drop_duplicates(subset=['name', 'subject'])
print(df_melt_drop_dup)

The output is as follows:

 Name Subject Grade
0 Xiao Ming Chinese score 86
1 Xiaogang Chinese score 92
2 Xiaohong Chinese score 88
3 Xiao Ming's math score is 90
4 Xiaogang Mathematics score 89
5 Xiaohong Math score 92
6 Xiao Ming's English score is 91
7 Xiaogang English score 86
8 Xiaohong English score 90

2. Multi-level index conversion

For multi-layer index data, we can use the Melt function to convert.

data_multi = {<!-- -->('Xiao Ming', 'Chinese'): 86,
              ('Xiaogang', 'Chinese'): 92,
              ('Xiaohong', 'Chinese'): 88,
              ('Xiao Ming', 'Mathematics'): 90,
              ('Xiaogang', 'Mathematics'): 89,
              ('Xiaohong', 'Mathematics'): 92,
              ('Xiao Ming', 'English'): 91,
              ('Xiao Gang', 'English'): 86,
              ('Xiaohong', 'English'): 90}

df_multi = pd.DataFrame(data_multi, index=[0]).T.reset_index()
df_multi.columns = ['name', 'subject', 'grade']
df_multi_melt = df_multi.melt(id_vars=['name', 'subject'], value_vars=['grade'], var_name='type', value_name='value')
print(df_multi_melt)

The output is as follows:

 Name Subject Type Value
0 Xiao Ming Chinese score 86
1 Xiaogang Chinese score 92
2 Xiaohong Chinese score 88
3 Xiao Ming's math score is 90
4 Xiaogang Mathematics score 89
5 Xiaohong math grade 92
6 Xiao Ming English score 91
7 Xiaogang English score 86
8 Xiaohong English score 90

3. Multi-column conversion

In some data analysis scenarios, it may be necessary to convert multiple columns into rows at the same time. At this time, multiple column names can be passed in the value_vars parameter.

data_multi_columns = {<!-- -->
    'A1': [0, 1, 2],
    'A2': [3, 4, 5],
    'B1': [6, 7, 8],
    'B2': [9, 10, 11],
    'C1': [12, 13, 14],
    'C2': [15, 16, 17]
}
df_multi_columns = pd. DataFrame(data_multi_columns)
df_multi_columns_melt = df_multi_columns.melt(value_vars=['A1', 'A2', 'B1', 'B2', 'C1', 'C2'], var_name=' column name', value_name='value')
print(df_multi_columns_melt)

The output is as follows:

 column name value
0 A1 0
1 A1 1
2 A1 2
3 A2 3
4 A2 4
5 A2 5
6 B1 6
7 B1 7
8 B1 8
9 B2 9
10 B2 10
11 B2 11
12 C1 12
13 C1 13
14 C1 14
15 C2 15
16 C2 16
17 C2 17

Conclusion

The Melt function is a very useful function in Python data analysis, which provides a way to convert a wide table into a long table or vice versa. In addition, it also supports a series of functions such as removing duplicate values, multi-level index data conversion, and multi-column conversion, which solves many problems faced in data analysis. When performing data analysis, mastering the use of the Melt function can improve the efficiency of data analysis and quickly build analysis reports.

The last last

This article is generated by chatgpt, and the article has not been modified on the basis of chatgpt. The above is just the tip of the iceberg of chatgpt capabilities. As a general Aigc large model, it just shows its original strength.

For ChatGPT, which subverts the way of working, you should choose to embrace rather than resist. The future belongs to those who “know how to use” AI.

AI Workplace Report Smart Office Copywriting Efficiency Improvement Tutorial Focus on AI + Workplace + Office direction.
The picture below is the overall syllabus of the course
img
img
The picture below is the ai tool used in the AI Workplace Report Smart Office Copywriting Efficiency Improvement Tutorial
img

High-quality tutorial sharing

  • You can learn more about artificial intelligence/Python related content! Just click the color font below to jump!
Learning route guidance (click to unlock) Knowledge positioning People positioning
AI workplace report smart office copywriting efficiency improvement tutorial Advanced level This course is the perfect combination of AI + workplace + office, Through ChatGPT text creation, one-click generation of office copywriting, combined with AI smart writing, easy to handle multi-scenario copywriting. Intelligently beautify PPT, and use AI to accelerate workplace reporting. AI artifact linkage, ten times increase the efficiency of video creation You create a quantitative trading system that is easy to expand, safer, and more efficient
Python actual WeChat ordering applet Advanced level This course is a perfect combination of python flask + WeChat applet, from project construction to Tencent Cloud deployment and online, to create a full-stack food ordering system.