Questions tagged [pandas-melt]

Use this tag for questions related to the pandas.melt or pandas.DataFrame.melt. Be sure to also include the [pandas] tag.

Melt can be used to unpivot a DataFrame from wide to long format.

Useful Canonical:

The documentation:

102 questions
0
votes
1 answer

Is there an easy way to handle wide timeseries data with pandas

I have a wide dataframe that looks like this. Hour Wed Aug 10 2022 Thu Aug 11 2022 Fri Aug 12 2022 Sat Aug 13 2022 0 1 52602 49281 52805 53069 1 2 49970 46938 …
franklin
  • 3
  • 2
0
votes
0 answers

How to combine melt, transpose and pivot functions in dataframe pandas without aggregation?

I have the following dataframe: Position Played Champion Result ID Side top riven 0 1 blue jg jax 0 1 blue top fiora 1 1 red jg lee sin 1 1 red top mundo 0 2 blue jg jax 0 2 blue top kayle 1 2 red jg olaf 1 2 red and I…
Yumi
  • 1
  • 1
0
votes
1 answer

Convert column with more than one string into rows with Pandas

I'm collecting some data from Google Sheets, but I need to convert one column with more than one string into a row. Each cell can have more than one string, and each string I want to put in one row I have this dataframe import pandas as pd df =…
Kardu
  • 865
  • 3
  • 13
  • 24
0
votes
1 answer

Pandas : Produce alternative rows with melt on multiple column

Say I have a dataframe data_dict = {'Number': {0: 1, 1: 2, 2: 3}, 'mw link': {0: 'SAM3703_2SAM3944 2', 1: 'SAM3720_2SAM4115 2', 2: 'SAM3729_2SAM4121_ 2'}, 'site_a': {0: 'SAM3703', 1: 'SAM3720', 2: 'SAM3729'}, 'name_a': {0: 'Chelak', 1:…
Himanshu Poddar
  • 7,112
  • 10
  • 47
  • 93
0
votes
0 answers

Why pandas.melt() not working on my dataframe?

I also visited official docs but same syntax is given. Here's my code. df = pd.DataFrame({'team': ['A', 'B', 'C', 'D'], 'points': [88, 91, 99, 94], 'assists': [12, 17, 24, 28], 'rebounds': [22, 28, 30,…
0
votes
1 answer

Pandas Getting first number of rows of each xls file

I have about (100 files +) XLS files in a folder with different columns names and data types. File_1.xls: Id test category 1 ab 4 2 cs 3 3 cs 1 .. .. FILE_2.xls: index remove stocks category 1 dr 4 …
Simon GIS
  • 1,045
  • 2
  • 18
  • 37
0
votes
1 answer

Using melt() in Pandas

Trying to melt the following data such that the result is 4 rows - one for "John-1" containing his before data, one for "John-2" containing his after data, one for "Kelly-1" containing her before data, and one for "Kelly-2" containing her after…
092374
  • 15
  • 1
  • 6
0
votes
3 answers

python dataframe melt with compound index

I have a dataframe like: index sample_1 sample_2 A/1 23 33 A/2 24 34 A/3 25 35 I want to convert…
ypriverol
  • 585
  • 2
  • 8
  • 28
0
votes
1 answer

How do I include many columns in pd.melt function instead of just typing each and everyone out?

I have a data set that has the following columns. funciton: pd.melt() years name date m1 m2 m3 m4 m5 m6 …. to m12 I set me variable name to month and try to include m1-m12, but I just cant get it to work. it will instead put everything in the new…
0
votes
2 answers

Transpose or melt to group by columns?

I have this data frame: df = pd.DataFrame({ "cluster" : ["A", "A", "A", "A", "A", "C", "C", "C", "C"], "col1": ["0", "0", "1", "1", "0", "1", "0", "1", "0"], "col2": ["1", "1", "1", "1", "0", "1", "1", "1",…
F_M
  • 301
  • 2
  • 8
0
votes
1 answer

DataFrame.melt() not pivoting columns

I have a CSV file that contains years in columns like this: I want to create one "year" column with the values in a new column. I tried using pandas.melt, but it doesn't seem to be changing the dataframe. Here is the relevant code: international_df…
AC1
  • 63
  • 1
  • 2
  • 7
0
votes
1 answer

Align not matching indices and perform pandas melt

I have two dataframes with different indices subject_ID,score,region,supplier 1001,27,AP,ABC 1002,35,AP,ABC 1003,29,ANZ,DEF 1004,35,ANZ,DEF 1005,30,KOREA,GHI 1006,34,KOREA,GHI df =…
The Great
  • 7,215
  • 7
  • 40
  • 128
0
votes
1 answer

Reshape DataFrame Pandas - some variables to long others to wide

I need to reshape a dataframe so that some of the variables (Diag1, Diag2, Diag3) change to long wile others (Period) change to wide. Basically they need to swap places. I have recreated the original dataframe in the example below. I've tried using…
John Conor
  • 722
  • 6
  • 20
0
votes
3 answers

Melt/Stack/Reshape multi header dataframe

I've scraped from data from excel that has more than one header row of data, with some nans in one row: df = pd.read_excel(file, header=None) 0 nan nan jan20 jan20 feb20 feb20 1 ID Name GrpA GrpB GrpA GrpB 2 1 One 1 2…
thesimplevoodoo
  • 153
  • 3
  • 11
0
votes
2 answers

Pivot wider to longer with value and unit (currency) columns

My data is in wide format as such: data = [{ id: '1', 'timestamp': '2021-10-01', 'product_type' : 'Quarterly', 'applicable_tariff_per_local_currency_kwh_d_value' : 1.11, 'applicable_tariff_per_local_currency_kwh_d_unit' :…