how to re-arrange multiple columns into one column with same index

Jay

I'm using python pandas and I want to adjust one same index to multiple columns and make it into one column. And when it's possible, I also want to delete the zero value.

I have this data frame

index A B C 
a     8 0 1 
b     2 3 0
c     0 4 0 
d     3 2 7 

I'd like my output to look like this

index data value
a     A    8 
b     A    2
d     A    3
b     B    3
c     B    4
d     B    2
a     C    1
d     C    7

===
I solved this task as below. My original data has 2 indexes & 0 in dataframe were NaN values.

At first, I tried to apply melt function while removing NaN values following this (How to melt a dataframe in Pandas with the option for removing NA values), but I couldn't. Because my original data has several columns ('value_vars'). so I re-organized dataframe by 2 steps:

  1. Firstly, I made multi-column into one-column by melt function,
  2. Then removed NaN values in each rows by dropna function.

Coding Script Snipppet

jdg

This looks a little like the melt function in pandas, with the only difference being the index.

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.melt.html

Here is some code you can run to test:

import pandas as pd
df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},'B': {0: 1, 1: 3, 2: 5},'C': {0: 2, 1: 4, 2: 6}})
pd.melt(df)

Image of the my jupter notebook

With a little manipulation, you could solve for the indexing issue.

This is not particularly pythonic, but if you have a limited number of columns, you could make due with:

molten = pd.melt(df)
a = molten.merge(df, left_on='value', right_on = 'A') 
b = molten.merge(df, left_on='value', right_on = 'B') 
c = molten.merge(df, left_on='value', right_on = 'C') 
merge = pd.concat([a,b,c])

Merge of the melted data frames

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to allow NULL value for one column in unique index on multiple columns

From Dev

how to select multiple same column in one column

From Dev

how to break one column into multiple columns in Linux

From Dev

How to merge multiple columns values into one column?

From Dev

How to combine multiple columns into one column in SQL?

From Dev

Multiple Columns in one column

From Dev

Multiple Columns in one column

From Dev

SELECT by one of multiple columns with index

From Dev

How to print columns with awk and in the same time edit just one column?

From Dev

How To Populate One Column Based On Another Columns Data In Same Table?

From Dev

How to separate same numbers from one column in different different columns?

From Dev

How to split a column into multiple columns with the index of the string with pandas?

From Dev

How to re-arrange QTableView's Columns Order

From Dev

Pandas: Multiple columns into one column

From Dev

Split one column into multiple columns

From Dev

Pandas: Multiple columns into one column

From Dev

Split one column into multiple columns

From Dev

Display one Column as Multiple Columns

From Dev

How can I combine multiple columns of the same data into a single column?

From Dev

How to collapse NA and merge multiple columns into one column

From Dev

How to divide one column in to multiple columns by other variables in R

From Dev

How to summarize multiple logical columns into one extra column?

From Dev

How to populate multiple columns based on different value in one column

From Dev

How to map multiple columns to one field using @Column annotation?

From Dev

How to filter multiple columns data to one column in Excel?

From Dev

how to re-arrange 3 for loops in a script to be just one

From Dev

How to check if the column value are the same among multiple record with one sql

From Dev

Merge multiple columns into one column with multiple rows

From Dev

Excel - INDEX function for multiple columns of same value

Related Related

  1. 1

    How to allow NULL value for one column in unique index on multiple columns

  2. 2

    how to select multiple same column in one column

  3. 3

    how to break one column into multiple columns in Linux

  4. 4

    How to merge multiple columns values into one column?

  5. 5

    How to combine multiple columns into one column in SQL?

  6. 6

    Multiple Columns in one column

  7. 7

    Multiple Columns in one column

  8. 8

    SELECT by one of multiple columns with index

  9. 9

    How to print columns with awk and in the same time edit just one column?

  10. 10

    How To Populate One Column Based On Another Columns Data In Same Table?

  11. 11

    How to separate same numbers from one column in different different columns?

  12. 12

    How to split a column into multiple columns with the index of the string with pandas?

  13. 13

    How to re-arrange QTableView's Columns Order

  14. 14

    Pandas: Multiple columns into one column

  15. 15

    Split one column into multiple columns

  16. 16

    Pandas: Multiple columns into one column

  17. 17

    Split one column into multiple columns

  18. 18

    Display one Column as Multiple Columns

  19. 19

    How can I combine multiple columns of the same data into a single column?

  20. 20

    How to collapse NA and merge multiple columns into one column

  21. 21

    How to divide one column in to multiple columns by other variables in R

  22. 22

    How to summarize multiple logical columns into one extra column?

  23. 23

    How to populate multiple columns based on different value in one column

  24. 24

    How to map multiple columns to one field using @Column annotation?

  25. 25

    How to filter multiple columns data to one column in Excel?

  26. 26

    how to re-arrange 3 for loops in a script to be just one

  27. 27

    How to check if the column value are the same among multiple record with one sql

  28. 28

    Merge multiple columns into one column with multiple rows

  29. 29

    Excel - INDEX function for multiple columns of same value

HotTag

Archive