Pivoting Dataframe with Pandas

jake wong

I have the below table taken online through pandas.read_html

      Column0    Column1      Column2      Column3
0     Entry_1    0.685        Record_1     0.69-S$ 0.685
1     Entry_2    0.036        Record_2     0.685
2     Entry_3    05/Jul/2016  Record_3     0.72-S$ 0.4
3     Entry_4    0.338        Record_4     178.8 mm
4     Entry_5    0.41         Record_5     0.06
5     Entry_6    122.48       Record_6     17.29%
6     Entry_7    0.5          Record_7     0.58 as of 05/Jul/2016

How do I pviot / transpose this data such that Column 0 becomes the headers, and Column 1 becomes the values. Similarly for Column 2 and Column 3?

Joe T. Boka

This is probably the easiest way to solve this problem. The easiest way I could come up with anyway.

Column0     Column1     Column2     Column3
0   Entry_1     0.685   Record_1    0.69-S$ 0.685
1   Entry_2     0.036   Record_2    0.685
2   Entry_3     05/Jul/2016     Record_3    0.72-S$ 0.4
3   Entry_4     0.338   Record_4    178.8 mm
4   Entry_5     0.41    Record_5    0.06
5   Entry_6     122.48  Record_6    17.29%
6   Entry_7     0.5     Record_7    0.58 as of 05/Jul/2016

cols = df['Column0'].append(df['Column2'])
vals = df['Column1'].append(df['Column3'])

newdf = pd.DataFrame(vals).T
newdf.columns = cols
newdf



 Entry_1    Entry_2     Entry_3     Entry_4     Entry_5     Entry_6     Entry_7     Record_1    Record_2    Record_3    Record_4    Record_5    Record_6    Record_7
0   0.685   0.036   05/Jul/2016     0.338   0.41    122.48  0.5     0.69-S$ 0.685   0.685   0.72-S$ 0.4     178.8 mm    0.06    17.29%  0.58 as of 05/Jul/2016

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pandas dataframe pivoting

From Java

Pivoting pandas dataframe by rank on id

From Dev

Pandas Dataframe Stacking versus Pivoting

From Dev

Resampling, grouping, pivoting a pandas dataframe

From Dev

Pivoting a pandas dataframe with duplicate index values

From Dev

Pivoting a Pandas dataframe with a gapless daterange as index

From Dev

pivoting pandas dataframe into prefixed cols, not a MultiIndex

From Dev

Pivoting a pandas dataframe to generate a (seaborn) heatmap

From Dev

How to efficiently columnize (=pivoting) pandas DataFrame (with groupby)?

From Dev

Pivoting pandas DataFrame -- AssertionError: Index length did not match values

From Dev

Pivoting a Pandas Dataframe containing strings - 'No numeric types to aggregate' error

From Dev

Pivoting data in pandas

From Dev

Pandas: Pivoting and plotting workflow

From Dev

Pandas pivoting with boolean

From Dev

dataframe slicing and pivoting then into multiple dataframes

From Dev

Multi-index pivoting in Pandas

From Java

Data wrangling with Python Pandas and pivoting

From Dev

Pivoting tables with duplicated data in pandas

From Dev

Pandas: Pivoting with multi-index data

From Dev

Pivoting (or reshaping) table in pandas into hierarchical columns

From Dev

Un-pivoting/Stacking a pivot table with Python Pandas

From Dev

pivoting pandas df - turn column values into column names

From Dev

Simple pivoting of DataFrame in python should work. Is this a bug when passing 'values' argument? If so, how to fix it?

From Dev

Simple pivoting of DataFrame in python should work. Is this a bug when passing 'values' argument? If so, how to fix it?

From Dev

Pivoting in sql

From Dev

How to pivoting

From Dev

Pivoting Integer

From Java

Pandas Dataframe

From Dev

Pandas Dataframe

Related Related

  1. 1

    Pandas dataframe pivoting

  2. 2

    Pivoting pandas dataframe by rank on id

  3. 3

    Pandas Dataframe Stacking versus Pivoting

  4. 4

    Resampling, grouping, pivoting a pandas dataframe

  5. 5

    Pivoting a pandas dataframe with duplicate index values

  6. 6

    Pivoting a Pandas dataframe with a gapless daterange as index

  7. 7

    pivoting pandas dataframe into prefixed cols, not a MultiIndex

  8. 8

    Pivoting a pandas dataframe to generate a (seaborn) heatmap

  9. 9

    How to efficiently columnize (=pivoting) pandas DataFrame (with groupby)?

  10. 10

    Pivoting pandas DataFrame -- AssertionError: Index length did not match values

  11. 11

    Pivoting a Pandas Dataframe containing strings - 'No numeric types to aggregate' error

  12. 12

    Pivoting data in pandas

  13. 13

    Pandas: Pivoting and plotting workflow

  14. 14

    Pandas pivoting with boolean

  15. 15

    dataframe slicing and pivoting then into multiple dataframes

  16. 16

    Multi-index pivoting in Pandas

  17. 17

    Data wrangling with Python Pandas and pivoting

  18. 18

    Pivoting tables with duplicated data in pandas

  19. 19

    Pandas: Pivoting with multi-index data

  20. 20

    Pivoting (or reshaping) table in pandas into hierarchical columns

  21. 21

    Un-pivoting/Stacking a pivot table with Python Pandas

  22. 22

    pivoting pandas df - turn column values into column names

  23. 23

    Simple pivoting of DataFrame in python should work. Is this a bug when passing 'values' argument? If so, how to fix it?

  24. 24

    Simple pivoting of DataFrame in python should work. Is this a bug when passing 'values' argument? If so, how to fix it?

  25. 25

    Pivoting in sql

  26. 26

    How to pivoting

  27. 27

    Pivoting Integer

  28. 28

    Pandas Dataframe

  29. 29

    Pandas Dataframe

HotTag

Archive