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
2
votes
1 answer

How do I further melt horizontal values into vertical values?

I have a dataframe which has horizontal identifiers (yes and no) and values, and I want to melt it into vertical values into each yes. Here is a snippet of my dataframe: option Region Store Name option1 option2 …
Aeternis
  • 23
  • 3
2
votes
1 answer

How can I combine 2 levels index into one in pandas?

I am trying to extract only the relevant information from a dataframe. My data looks like import pandas as pd import numpy as np df = pd.DataFrame({'ID': {0: 'id1', 1: 'id1', 2: 'id1'}, 'EM': {0: 'met1', 1: 'met2', 2: 'met3'}, …
Amilovsky
  • 397
  • 6
  • 15
2
votes
4 answers

Make a wide dataframe long and add columns according to another column's name

I need to use some names of the columns as part of the df. While keeping the first 3 columns identical, I need to create some other columns based on the content of the row. Here I have some transactions from some customers: cust_id cust_first …
AnaMil
  • 23
  • 3
2
votes
3 answers

I am trying to Stack, Melt, grouby or reshape a Pandas DataFrame

I am trying to reshape the dataframe below (imported from a .csv), keeping the Easting, Northing and Node Name value together on the same row, but having everything 'stacked' in 4 columns. So I want data in columns V0e, V0n and Vd on top of data in…
WillH
  • 281
  • 2
  • 13
2
votes
3 answers

How do I melt a pandas with custom nam

I have a table like this device_type version pool testMean testP50 testP90 testP99 testStd WidgetMean WidgetP50 WidgetP90 WidgetP99 WidgetStd PNB0Q7 8108162 123 124 136 140.8 141.88 21.35 2.2 0 6.4 9.64 3.92 I want it to be transformed…
Aditya
  • 818
  • 1
  • 10
  • 21
2
votes
3 answers

Grouping several dataframe columns based on another columns values

I have this dataframe: refid col2 price1 factor1 price2 factor2 price3 factor3 0 1 a 200 1 180 3 150 10 1 2 b 500 1 450 3 400 10 2 3 c 700 …
imatiasmb
  • 113
  • 7
2
votes
1 answer

Melt on pandas dataframes with multi-level

I have a data table that looks like this: Original Table Importing this into pandas using df = pd.read_excel('H:/test/test.xlsx', header=[0,1,2]) gives this dataframe: Day Cats Dogs Unnamed:…
jski212002
  • 83
  • 1
  • 7
2
votes
1 answer

How to transform pandas df containing boolean columns into a heatmap compatible table?

I have a table something like this: A B C 1 1 1 1 2 0 1 1 3 0 0 1 Where each cell is containing whether the row is connected to the specified column. I want to transform this into a table like…
2
votes
4 answers

How to unpivot dataset in Python

I have a dataset which I need to unpivot into multiple rows into columns. Eg: ID Currency Val1 Val2 Month 101 INR 57007037.32 1292025.24 2021-03 101 INR 49171143.9 1303785.98 2021-02 101 INR 54039073.81 1469727.23 …
Rummy
  • 51
  • 8
1
vote
3 answers

How can I transform a DataFrame using pivot and melt in Python Pandas?

I have a DataFrame date price num_floors house 1 2023-01-01 94.30076 3 A 2 2023-01-01 95.58771 2 B 3 2023-01-02 102.78559 1 C 4 2023-01-03 93.29053 3 D and I…
jonas Chen
  • 11
  • 2
1
vote
1 answer

Unpivot yahoo finance multiple ticker download

I am trying to unpivot a yahoo finance download. import yfinance as yf import pandas as pd from datetime import date today = date.today() data = yf.download("AAPL WMT", start="2023-05-01", end=today).round(2) Running this script I…
Stix
  • 39
  • 6
1
vote
2 answers

Data Preparation Advanced Pandas Melt Multiple Column Sets. Merge some Columns to make them one

I have a dataset like: Mileage Operator 1 16 Seater 29-seater Operator 2 16-seater.1 29-seater.1 80 BusRus £199 £299 Wheels £190 £290 I want it in the following format to build a price prediction model with Operator and Seats as…
1
vote
1 answer

Turn the column names of a pandas df into a single column feature (with many nans)

pd.DataFrame({'apple': [np.nan, 'Bad', 'Good'], 'banana': [np.nan, np.nan, np.nan], 'orange': ['Good', np.nan, np.nan]}) Right now, I have a pd dataframe in the format similar to below: apple banana orange 0 NaN NaN Good 1 Bad …
sharkbirth
  • 17
  • 4
1
vote
4 answers

Melt four (or more) dataframe columns into two rows

I am trying to convert a dataframe of structure: ID ID2 ID3 R_u L_u R_sd L_sd 1 F G1 x y z t 2 M G2 x y z t into ID ID2 ID3 Side u sd 1 F G1 R x z 1 F G1 L y t 2 M G2 R x z 2 M G2 L y t I…
SAM
  • 55
  • 1
  • 7
1
vote
1 answer

regex column selection inside pd.melt function

I have a very wide df with about 100 columns, within which there are several 'XYZ_rating' columns that I want to pivot longer. df = pd.DataFrame({ 'id': [1, 1, 1, 2, 2, 2, 3, 3, 3], 'first_rating': [1, 2, 3, 1, 2, 3, 1, 2, 3], …
prayner
  • 393
  • 1
  • 10