CodingMatters
CodingMatters

Reputation: 1431

reshaping pandas dataframe from m rows n columns to m x n rows with one column of values

reshape the data frame from a m rows x n columns to a m x n rows single column.

Year   Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov  Dec
2000  12.7  13.8  13.3  12.6  12.8  12.3  13.4    14    13  12.8    13 13.2
2001  13.8  13.7  13.8  13.9  13.4  14.2  14.4    15.6  15.2    16  15.9    17
2002  16.5    16  16.6  16.7  16.6  16.7  16.8    17    16.3  15.1  17.1  16.9

to

Year Month Value
2000 Jan   12.7
2000 Feb   13.8
2000 Mar   13.3

then easy combine 'Year'+'Month' columns into a datefield and plot the data column.

I'm rusty as heck on this. Reading the various melt, reshape, stack options is frustratingly slow.

Upvotes: 0

Views: 340

Answers (2)

busybear
busybear

Reputation: 10580

As others have mentioned, melt will work if Year is a column in your dataframe. If Year is your index (hard to tell, though likely based on your dimensions), you can use stack and reset_index instead.

to_datetime can combine your two columns to create a datetime, but you'll need to assign a day value first:

df.columns.name = 'Month'
df = df.stack().reset_index()
df['Date'] = pd.to_datetime(df[['Year', 'Month']].assign(Day=1))

The new date column can be used to plot with the package you prefer.

Upvotes: 0

Binh
Binh

Reputation: 1173

You can use melt for dataframe of pandas like this:

df = pd.melt(df, id_vars=['Year'])

All the other column name which is not specified in id_vars will be melt to one column for you

Upvotes: 1

Related Questions