0% found this document useful (0 votes)
74 views3 pages

Pandas Melt Function

The document discusses the pandas melt function. [1] It loads weather data for different cities and displays the dataframe. [2] It uses melt to reshape the dataframe, setting the 'day' column as the id variable and creating 'variable' and 'value' columns. [3] It then accesses the melted data for specific cities. [4] Finally, it renames the 'variable' and 'value' columns to more descriptive names of 'City' and 'Temperature'.

Uploaded by

jose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views3 pages

Pandas Melt Function

The document discusses the pandas melt function. [1] It loads weather data for different cities and displays the dataframe. [2] It uses melt to reshape the dataframe, setting the 'day' column as the id variable and creating 'variable' and 'value' columns. [3] It then accesses the melted data for specific cities. [4] Finally, it renames the 'variable' and 'value' columns to more descriptive names of 'City' and 'Temperature'.

Uploaded by

jose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Pandas - Melt

January 8, 2023

1 Pandas Melt Function


[1]: import pandas as pd
df = pd.read_csv('weather.csv')
df.head()

[1]: day Dhaka Sylhet Chittagong


0 Monday 20 21 21
1 Tuesday 20 26 26
2 Wednesday 25 27 26
3 Thursday 20 27 22
4 Friday 26 22 27

[4]: df1 = pd.melt(df, id_vars=['day']) # id_vars = id variable, x axix or column,␣


↪melt works as pivot to some extend

df1

[4]: day variable value


0 Monday Dhaka 20
1 Tuesday Dhaka 20
2 Wednesday Dhaka 25
3 Thursday Dhaka 20
4 Friday Dhaka 26
5 Saturday Dhaka 20
6 Sunday Dhaka 26
7 Monday Sylhet 21
8 Tuesday Sylhet 26
9 Wednesday Sylhet 27
10 Thursday Sylhet 27
11 Friday Sylhet 22
12 Saturday Sylhet 20
13 Sunday Sylhet 25
14 Monday Chittagong 21
15 Tuesday Chittagong 26
16 Wednesday Chittagong 26
17 Thursday Chittagong 22
18 Friday Chittagong 27
19 Saturday Chittagong 23

1
20 Sunday Chittagong 23

[7]: df1[df1['variable']=='Dhaka'] # Accessing

[7]: day variable value


0 Monday Dhaka 20
1 Tuesday Dhaka 20
2 Wednesday Dhaka 25
3 Thursday Dhaka 20
4 Friday Dhaka 26
5 Saturday Dhaka 20
6 Sunday Dhaka 26

[8]: df1[df1['variable']=='Sylhet'] # Accessing

[8]: day variable value


7 Monday Sylhet 21
8 Tuesday Sylhet 26
9 Wednesday Sylhet 27
10 Thursday Sylhet 27
11 Friday Sylhet 22
12 Saturday Sylhet 20
13 Sunday Sylhet 25

1.0.1 Documentation
https://pandas.pydata.org/docs/reference/api/pandas.melt.html

Changing Column Names


[10]: df1 = pd.melt(df, id_vars=['day'], var_name = 'City', value_name =␣
↪'Temperature')

df1

[10]: day City Temperature


0 Monday Dhaka 20
1 Tuesday Dhaka 20
2 Wednesday Dhaka 25
3 Thursday Dhaka 20
4 Friday Dhaka 26
5 Saturday Dhaka 20
6 Sunday Dhaka 26
7 Monday Sylhet 21
8 Tuesday Sylhet 26
9 Wednesday Sylhet 27
10 Thursday Sylhet 27
11 Friday Sylhet 22
12 Saturday Sylhet 20
13 Sunday Sylhet 25

2
14 Monday Chittagong 21
15 Tuesday Chittagong 26
16 Wednesday Chittagong 26
17 Thursday Chittagong 22
18 Friday Chittagong 27
19 Saturday Chittagong 23
20 Sunday Chittagong 23

You might also like