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
1
vote
1 answer

Python - rearranging dataframe data

I wanna rearrange my dataframe from the left one to the right table, like I show you in the next picture: df = pd.DataFrame({ "Unnamed:0": ["Entity","","Var1","Var2","Var3","Var4"], "Unnamed:1": ["A","X","0.45","0.14","0.16","0.28"], "Unnamed:2":…
Edu2694
  • 29
  • 4
1
vote
1 answer

How to Melt a column into another melted column within Pandas?

I have a file consisting of sales for different items. I have a column of model predictions named outputs and which model produced those predictions named, model. I need to take the current model's (that's in production) predictions in a column…
Jordan
  • 1,415
  • 3
  • 18
  • 44
1
vote
1 answer

Python melt with same column in id_vars and value_vars

I have following dataframe df: data = {'SAP ID': ['MATSAP1', 'MATSAP2'], 'DESCRIPTION': ['DESC1', 'DESC2'], 'BASIC': ['BD1', 'BD2'], 'LINK': ['MD1', 'MD2']} df = pd.DataFrame(data) I want to melt the df and have the following…
1
vote
1 answer

Pivoting pandas dataframe without requiring the row index to be unique

I currently have a multi-dimensional index dataframe as follows: Placed Placed Placed Attachment Attachment Attachment Limit Limit Limit Treaty…
Tommo
  • 35
  • 5
1
vote
1 answer

Pandas -need to change shape of dataframe to different one

enter code herecould you help to solve one task. I have data frame 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:…
Dilshod
  • 11
  • 2
1
vote
1 answer

Change columns to rows per student ID

I have data in excel sheet that I am reading into a dataframe: ID Grade Course Q1 Number Q1 Letter Q2 Number Q2 Letter 1 9 English 73 B 69 C 1 9 Math 70 B 52 C 1 9 Science 69 C 80 A desired output: ID Grade Course Semester Number…
1
vote
3 answers

keeping first column value .melt func

I want to use dataframe.melt function in pandas lib to convert data format from rows into column but keeping first column value. I ve just tried also .pivot, but it is not working good. Please look at the example below and please help: ID …
matheo_eo
  • 13
  • 2
1
vote
1 answer

Reshape data frame, so the index column values become the columns

I want to reshape the data so that the values in the index column become the columns My Data frame: Gender_Male Gender_Female Location_london Location_North Location_South Cat V 5 4 4 …
SAJ
  • 368
  • 1
  • 2
  • 14
1
vote
1 answer

using pandas.melt for big dataframe

I am using at some point pd.melt to reshape my dataframe. This command after inspections is taking around 7min to run which is too long in my use case (I am using it in an interactive dashboard). I am asking if there are any methods on how to…
Triki Sadok
  • 120
  • 7
1
vote
1 answer

Pandas groupby issue after melt bug?

Python version 3.8.12 Pandas 1.4.1 Given the following dataframe: import pandas as pd import numpy as np df = pd.DataFrame({ 'id': [1000] * 4, 'date': ['2022-01-01'] * 4, 'ts': pd.date_range('2022-01-01', freq='5M', periods=4), 'A':…
Serg
  • 121
  • 1
  • 9
1
vote
1 answer

Reshape Pandas DataFrame with TimeSeries in rows instead of columns

I have a DataFrame df that contains price data (Open, Close, High, Low) for every day in the time from January 2010 to December 2021: Name ISIN Data 02.01.2010 05.01.2010 06.01.2010 ... 31.12.2021 Apple US9835635986 Price…
LukeMahn
  • 95
  • 6
1
vote
3 answers

Convert x same size 2D numpy arrays to a 2+x column data frame

I have two ndarrays of size (m x n), and two lists of length m and n respectively. I want to convert the two matrices to a dataframe with four columns. The first two columns correspond to the m and n dimensions, and contain the values from the…
MDescamps
  • 67
  • 1
  • 6
1
vote
1 answer

Counting unique values cumulatively by date in pandas and add a column

I have the following data. I want to create a column which is a cumulative sum of number of unique values from both "from" and "to" columns by date. Current dataframe id date from to A 01/01/2020 alice bob A 01/01/2020…
Olive
  • 644
  • 4
  • 12
1
vote
1 answer

Pandas Reshape dataframe without using melt function

I have a dataframe as : i need to convert it as final result like this I need the rows name and id with value 1 as according on last 3 columns as shown in the image. It can be done using pandas melt function but i was looking forward to do without…
Rudra
  • 223
  • 2
  • 13
1
vote
1 answer

How to compare 2 Dataframes on set columns using Groupby where the data is not organized ideally in one of them

The problem that I am attempting to solve is to reconcile rates applied to the actual accounts shown in "AccountTable" versus what the should be set in "RateTable". For each Account its rate can be set at different levels, either at the Account…
Alan Paul
  • 91
  • 6