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
3 answers

Pandas: how to unpivot df correctly?

I have the following dataframe df: A B Var Value 0 A1 B1 T1name T1 1 A2 B2 T1name T1 2 A1 B1 T2name T2 3 A2 B2 T2name T2 4 A1 B1 T1res 1 5 A2 B2 T1res 1 6 A1 B1 T2res 2 7 A2 B2 T2res 2 I now want to 'half' my dataframe because Var…
Zizzipupp
  • 1,301
  • 1
  • 11
  • 27
0
votes
2 answers

dataframe how pivot table based on substring of the column

I have a dataframe: df = time id ser1 ser2 ... ser20 N0ch0 N1ch0 N2ch0 N0ch1 N1ch1 N2ch1 N0ch2 N1ch2 N2ch2 N0ch3 N1ch3 N2ch3 1 2 4 5 3 8 7 8 5 1 4 6 2 7 9 8 6 And I want to pivot it…
Cranjis
  • 1,590
  • 8
  • 31
  • 64
0
votes
1 answer

Split column into multiple columns when a row starts with a string

I am wanting to split a single column, in a dataframe, into multiple columns whenever a row in the column startswith a specific string. I have a dataframe with a single column like here: import pandas as pd lst = ['Group 123 nv-1', 'a, v', 's,b',…
BPhillips
  • 7
  • 3
0
votes
1 answer

Reshaping pandas df from corsstabulation to long format

I have a dataframe along the lines of the below: d = {'Country': ['USA', 'France', 'Germany'], 'USA': ['NaN', 2, 5], 'France': [3, 'NaN', 7], 'Germany': [3.5, 4, 'NaN']} df = pd.DataFrame(data=d) Country USA France Germany 0 USA NaN 3…
0
votes
1 answer

list and print it out under each other

I have a problem. I have a list with all amenities of a house. I want to split that list and print it out under each other with pandas melt. I want to use the function melt. I tried that but my result don't looks like what I want. d = {'hostid': [1,…
Mr. Hankey
  • 954
  • 1
  • 5
  • 12
0
votes
0 answers

Pandas - pandas melt multiindex columns

This is what I received from spreadsheet: import pandas as pd import numpy as np arrays = [['Phase 1','Phase 1','Phase 1','Phase 1','Phase 1','Phase 1','Phase 1','Phase 1','Phase 1'], ['Function A','Function A','Function A','Function…
mgrijo
  • 1
  • 1
0
votes
0 answers

Long to Wide unpivot with multiple categorical variables in pandas

I have a DataFrame that when simplified looks like the following. import pandas as pd report_date_rng = pd.DataFrame(pd.date_range(start='2021-08-01', end='2021-09-14', freq='7D'), columns=['date']) inp = [{'Store':'A', 'Season':'Summer',…
bkeesey
  • 466
  • 4
  • 12
0
votes
1 answer

Pivot Pandas Python Dataframe

I'm working on this dataset: # dummy data import pandas as pd data = pd.DataFrame({None : ['Company', 'AAA', 'BBB', 'CCC','Company', 'AAA' ], None : ['Copper', 'Copper', 'Iron', 'Iron', 'Gold', 'Gold'], …
Luigi
  • 59
  • 1
  • 6
0
votes
1 answer

Numbering for Rows generated through comma separated Pandas DataFrame

I have a Pandas DataFrame as follows: +----------+---------------+-----------+---------------+ | List No. | List Item No. | Item Name | Issues | +----------+---------------+-----------+---------------+ | 1 | 1 | A |…
Lopez
  • 461
  • 5
  • 19
0
votes
1 answer

groupby and melt Function of COVID-19 URL data with Python

I created a function that read COVID-19 CSV file from a URL, drop some features, and groupby a Country/ Region names. I want this function to use DataFrame.melt to keep 'Country/Region' constant and bring all the dates that existed in rows as a…
Negin Zarbakhsh
  • 167
  • 1
  • 3
  • 13
0
votes
2 answers

Reshape multiples variables with melt with python, Pandas

I have survey date with 5 main variables f1_, f2_, f3_ ,f4_ and f5_ and each f*_ group variable has up to 10 sub groups, ex: f1_1 , f1_2 ,f1_3 ... or f2_1, f2_2, ... f2_10. I would like to perform a pivot_longer to reshape my dataframe in order to…
DanG
  • 689
  • 1
  • 16
  • 39
-1
votes
0 answers

pandas melt gives ValueError if value_name is same as a column name

I have a dataframe that I am using melt that gives me a ValueError in pandas2. Any idea why I get this. This code works fine in older versions though from pandas import DataFrame data = DataFrame({'A': ['a1', 'a2', 'a3'], 'B': ['b1', 'b1', 'b2'],…
bhamu
  • 121
  • 6
1 2 3 4 5 6
7