how to split a pandas dataframe from wide to tall shape

user_dhrn

I have a dataframe containing this structure and I have figured out how to 'unpivot' the df by doing this, but I am pretty sure is not the more pythonic way I would like to have. Can you please suggest a better way to do it?:

v = [[{'BIN_ID_WDM': i, 'DSIMILARITY': df1.D1[i], 'BIN_ID_IHS': df1.ID1[i]},
      {'BIN_ID_WDM': i, 'DSIMILARITY': df1.D2[i], 'BIN_ID_IHS': df1.ID2[i]},
      {'BIN_ID_WDM': i, 'DSIMILARITY': df1.D3[i], 'BIN_ID_IHS': df1.ID3[i]},
      {'BIN_ID_WDM': i, 'DSIMILARITY': df1.D4[i], 'BIN_ID_IHS': df1.ID4[i]},
      {'BIN_ID_WDM': i, 'DSIMILARITY': df1.D5[i], 'BIN_ID_IHS': df1.ID5[i]}]
      for i in df1.index]

dataframe:

    D1  D2  D3  D4  D5  ID1 ID2 ID3 ID4 ID5
WMAC                                        
258403  0.002665    0.003306    0.001396    0.003395    0.003741    100000141725    100000141709    100000141696    100000141676    100000141294
105692  0.000016    0.000257    0.000264    0.000298    0.000349    100000030110    100000030243    100000030109    100000030166    100000323212
70795   0.001588    0.001564    0.000019    0.001828    0.001828    100000040111    100000028683    100000034744    100000324405    100000038952
Scott Boston

IIUC, use pd.wide_to_long:

pd.wide_to_long(df, ['D', 'ID'], 'WMAC', 'No')

Output:

                  D            ID
WMAC   No                        
258403 1   0.002665  100000141725
105692 1   0.000016  100000030110
70795  1   0.001588  100000040111
258403 2   0.003306  100000141709
105692 2   0.000257  100000030243
70795  2   0.001564  100000028683
258403 3   0.001396  100000141696
105692 3   0.000264  100000030109
70795  3   0.000019  100000034744
258403 4   0.003395  100000141676
105692 4   0.000298  100000030166
70795  4   0.001828  100000324405
258403 5   0.003741  100000141294
105692 5   0.000349  100000323212
70795  5   0.001828  100000038952

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to change the shape of a Pandas Dataframe (row number with an "L")?

分類Dev

How can I split a pandas DataFrame into multiple dataframes?

分類Dev

Pandas melt function from wide to long

分類Dev

How to create a Pandas DataFrame from a list of OrderedDicts?

分類Dev

How to convert from Pandas' DatetimeIndex to DataFrame in PySpark?

分類Dev

How to stay with a percent of data from a pandas DataFrame?

分類Dev

How to update a pandas dataframe with sets, from another dataframe

分類Dev

How to go from long to wide structure in HIVE?

分類Dev

Split a pandas dataframe header into multiple columns

分類Dev

How to split the columns of a dataframe by their name?

分類Dev

How to get percentage from how filled a pandas dataframe column is?

分類Dev

Why does the shape of the selection of my pandas dataframe is wrong

分類Dev

Efficiently finding shape over multiple rows in pandas dataframe

分類Dev

How to extract city name with rege from team name in pandas dataframe

分類Dev

How to update a pandas dataframe, from multiple API calls?

分類Dev

How to update a pandas dataframe, from multiple API calls?

分類Dev

How to extract non NA values in a list or dict from a pandas dataframe

分類Dev

How to extract city,state from a column in pandas dataframe?

分類Dev

How to read large data set from mongodb to pandas dataframe

分類Dev

How to compare the data and choose the TOP 2 from multiIndex dataframe in pandas?

分類Dev

How to create input samples from pandas dataframe for a LSTM model?

分類Dev

how to strip unwanted text from column in pandas dataframe

分類Dev

How to load a Pandas DataFrame from a csv/tsv as factorize category type?

分類Dev

How to save pandas dataframe output from function to workspace?

分類Dev

How to plot simple plot from DataFrame in Python Pandas?

分類Dev

How to concatenate a specific column from a pandas.DataFrame()?

分類Dev

How to find harmonic average speeds from pandas dataframe

分類Dev

How to drop rows from dataframe after checking split text membership in a list?

分類Dev

How to subdivide a pandas dataframe

Related 関連記事

  1. 1

    How to change the shape of a Pandas Dataframe (row number with an "L")?

  2. 2

    How can I split a pandas DataFrame into multiple dataframes?

  3. 3

    Pandas melt function from wide to long

  4. 4

    How to create a Pandas DataFrame from a list of OrderedDicts?

  5. 5

    How to convert from Pandas' DatetimeIndex to DataFrame in PySpark?

  6. 6

    How to stay with a percent of data from a pandas DataFrame?

  7. 7

    How to update a pandas dataframe with sets, from another dataframe

  8. 8

    How to go from long to wide structure in HIVE?

  9. 9

    Split a pandas dataframe header into multiple columns

  10. 10

    How to split the columns of a dataframe by their name?

  11. 11

    How to get percentage from how filled a pandas dataframe column is?

  12. 12

    Why does the shape of the selection of my pandas dataframe is wrong

  13. 13

    Efficiently finding shape over multiple rows in pandas dataframe

  14. 14

    How to extract city name with rege from team name in pandas dataframe

  15. 15

    How to update a pandas dataframe, from multiple API calls?

  16. 16

    How to update a pandas dataframe, from multiple API calls?

  17. 17

    How to extract non NA values in a list or dict from a pandas dataframe

  18. 18

    How to extract city,state from a column in pandas dataframe?

  19. 19

    How to read large data set from mongodb to pandas dataframe

  20. 20

    How to compare the data and choose the TOP 2 from multiIndex dataframe in pandas?

  21. 21

    How to create input samples from pandas dataframe for a LSTM model?

  22. 22

    how to strip unwanted text from column in pandas dataframe

  23. 23

    How to load a Pandas DataFrame from a csv/tsv as factorize category type?

  24. 24

    How to save pandas dataframe output from function to workspace?

  25. 25

    How to plot simple plot from DataFrame in Python Pandas?

  26. 26

    How to concatenate a specific column from a pandas.DataFrame()?

  27. 27

    How to find harmonic average speeds from pandas dataframe

  28. 28

    How to drop rows from dataframe after checking split text membership in a list?

  29. 29

    How to subdivide a pandas dataframe

ホットタグ

アーカイブ