How can I transform a dataframe by putting different values of a column into different columns?

C Cheng

I have a question about my code. My data looks like this:

enter image description here

I want to summarize by SKU # and by reason what percentage (VS Ordered) was not shipped to this format:

enter image description here

What I did is that I used pd.groupby to have the sum of cut by SKU#, then I got a dataframe looks like this: SKU Reason A Transportation A Raw Material B Transportation B Raw Material C Transportation C Raw Material

Just wondering how can I change to this format: A Transportation Raw Material B Transportation Raw Material C Transportation Raw Material

My plan is to merge this one to one that was sum of order amount by SKU number to get the percentages of each reason. I would be more than happy to get a better method as well!

dww142

Create a ratio of unshipped, then sum in a pivot table:

data = {
    'PO Nbr' : [1,2,3,4,5],
    'SKU #' : ['A','B','A','C','A'],
    'Ordered' : [100,100,500,100,200],
    'Shipped' : [100,80,450,100,30],
    'Reason for Not Shipped' : ['', 'raw material','transportation','','transportation']
}

df = pd.DataFrame(data)

df['Pct Not Shipped'] = (df['Ordered'] - df['Shipped']) / df.groupby('SKU #').Ordered.transform('sum')

print(df.pivot_table(index='SKU #',columns='Reason for Not Shipped',values='Pct Not Shipped',aggfunc='sum'))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I transform a DataFrame so that the headers become column values?

Putting comma separated values into separate or different columns

How can I insert a column into a dataframe if the column values come from a different file?

How to transform a column value of a dataframe with values of another dataframe columns

How can we replace the columns values of one dataframe based on different dataframe column using some conditions?

How do I create a new column for my dataframe whose values are maps made up of values from different columns?

How do I rearrange values in different columns in an R dataframe

How can I slice elements of one Pandas dataframe column by different values?

How can I add a calculated column with different rows to a dataframe?

How can I make different columns using only one column in a dataframe?

How can i get column values in different language in MySQL?

How can I manipulate dataframe columns with different values from an external vector (with dplyr)

How to replace different strings with different values pretty in a column of dataframe by pandas?

Oracle SQL How can I separate values from a column in two different columns?

How can I replace a column of values by another with different number of rows?

How can I count occurences of different integer values (in a particular column) for rows with different factor values(another column)?

How can I create a NEW column in a dataframe based on values of another column in a DIFFERENT dataframe that have common information?

Pandas: How can I remove duplicates with single column different values, while retaining said different values

How to transform all values in a column of a pandas dataframe into new columns with counts?

How can I fill a column based on a difference between the values of two different columns, using groupby?

How can I count the amount of values in different columns in oracle plsql

How can I groupby a DataFrame at the same time I count the values and put in different columns?

How can I create a column target based on two different columns?

After comparing two columns, how can I remove unique values in one column with its values in other different columns in R

How can I find out same values with more than one condition and put values of the same column in different columns?

how I can split a column of a pyspark dataframe with different size of whitspace?

How can I subtract specific values with different observations in the same Dataframe

how i can replace different values in a column to category based on their values

How can I check in a pandas dataframe for a different values in different columns at the same time?