Pandas Pivot - remove multi level index and set as columns [duplicate] - python

This question already has answers here:
transform pandas pivot table to regular dataframe
(3 answers)
Closed 1 year ago.
Hi I am using pandas pivot functionality .
df1=pd.pivot_table(df, index=[week_column,'Rank_Trx'],columns='Target channel',values= [id_column],aggfunc=pd.Series.nunique)
The output I receive is something like this.
However the output i am looking for should look like this.
So the columns of the data frame I wish to have should be "Period","Rank_Trx","PDE","Samples","Take no action"
Basically I want this a NxN data frame without any multilevel index . I am using the latest pandas version 1.0.5

You can reset the index of the dataframe with
df1 = df1.reset_index()

Related

pandas date_range dataframe to column headers [duplicate]

This question already has answers here:
How to switch columns rows in a pandas dataframe
(2 answers)
Closed 7 months ago.
Im new to python, and im attempting to create a date_range, convert the date_range to a DataFrame and convert each DataFrame row into a header. I have been perusing through the interwebs and cannot find a solution. It seems a simple problem, but i guess im to new to implement a simple solution. Any help is apreciated.
Here is what i have:
duration = pd.date_range(start='1/1/2022', periods=52, freq='W')
df = pd.DataFrame({'Date': duration})
Result:
RESULT
Need code for desired result:
Expected Result
Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. The property T is an accessor to the method transpose().

Reverse explode with dummy df [duplicate]

This question already has answers here:
Reversing 'one-hot' encoding in Pandas
(9 answers)
Closed 1 year ago.
I've been trying to use reverse explode from here: How to implode(reverse of pandas explode) based on a column
But I have a little bit different df.
I have df looking like this:
I need to 'reverse explode' it, but I couldn't find any option to groupby by index. Is there any option to do that?
To be precise, I need all columns to remain, but all '1' should be combined in a row.
I merged dummy df with main df, but can not figure out what to do next.
rest_cuisine_style = pd.concat([rest_cuisine_style, cuisine_dummies], axis=1)
Does this work?
rest_cuisine_style = rest_cuisine_style.idxmax(axis=1)

convert specific columns values to column_names in pandas [duplicate]

This question already has answers here:
How can I pivot a dataframe?
(5 answers)
How to pivot a dataframe in Pandas? [duplicate]
(2 answers)
Closed 1 year ago.
I have a dataframe like this:
index,col1,value
1,A,1
1,B,2
2,A,3
2,D,4
2,C,5
2,B,6
And I would like to convert this dataframe to this:
index,col1_A,col1_B,col1_C,col1_D
1,1,2,np.Nan,np.nan
2,3,4,5,6
The conversion is based on the index column value and for each unique index column, the column values from col1 is converted to column name and its associated value is set to the corresponding value available in value columns.
Currently my solution contains looping by creating subset of df as temporary df based on each index and then starting looping there. I am wondering if there is already builtin solution available for it in pandas. please feel free to suggest.

How to pivot a pandas Dataframe in Python? [duplicate]

This question already has answers here:
How to switch columns rows in a pandas dataframe
(2 answers)
How can I pivot a dataframe?
(5 answers)
Closed 4 years ago.
I am trying to pivot the below dataframe. I want the column names to be added as rows. First row is a statis one but the Column names are not static since they will be calculated for the all numerical columns from the data frame. Could you please help.
This is my data frame:
Expected Dataframe:
You just add .T :) df.describe().T to transpose your results:
import pandas as pd
import numpy as np
#Create a Dictionary of series
d = {'Name':pd.Series(['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine',
'Rahul','David','Andrew','Ajay','Teresa']),
'Age':pd.Series([26,27,25,24,31,27,25,33,42,32,51,47]),
'Score':pd.Series([89,87,67,55,47,72,76,79,44,92,99,69])}
#Create a DataFrame
pd.DataFrame(d).describe().T
Results:
You can transpose the dataframe:
data_pivot = data_pd.T
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.transpose.html
Other way is .transpose():
data_pivot = data_pd.transpose()

Pandas: Return a new Dataframe with specific non continuous column selection [duplicate]

This question already has answers here:
How to take column-slices of dataframe in pandas
(11 answers)
Closed 6 years ago.
I have a dataframe with 85 columns and something like 10.000 rows.
The first column is Shrt_Desc and the last Refuse_Pct
The new data frame that I want has to have Shrt_Desc, then leave some columns out and then include in series Fiber_TD_(g) to Refuse_Pct
I use:
dfi_3 = food_info.loc[:, ['Shrt_Desc', 'Fiber_TD_(g)':'Refuse_Pct']]
but it gives a syntax error.
Any ideas how can I achieve this?
Thank you.
Borrowing the main idea from this answer:
pd.concat([food_info['Shrt_Desc'], food_info.ix[:, 'Fiber_TD_(g)':]], axis=1)

Categories

Resources