Pandas - transpose one column

JesusMonroe

I'm having difficulty using transpose with pandas.

I have the following df:

date         name    quantity
1/1/2018     A       5
1/1/2018     B       6
1/1/2018     C       7
1/2/2018     A       9
1/2/2018     B       8
1/2/2018     C       6

I eventually want to create a pairwise correlation for all the names and their quantities on each date. To to that end, I'm trying to create the following output from this df first:

 date       A    B    C
 1/1/2018   5    6    7
 1/2/2018   9    8    6

The transpose is difficult to me since I can get duplicate column headers, but I also don't want to lose any data by dropping them first. I have a feeling the answer may be with a panda utility that I don't really use and I may be tunneling on transpose...

jpp

Since you aren't performing an aggregation, pd.DataFrame.pivot should be preferred to groupby / pivot_table:

res = df.pivot(index='date', columns='name', values='quantity')

print(res)

name      A  B  C
date             
1/1/2018  5  6  7
1/2/2018  9  8  6

If you wish you can use reset_index to elevate date to a column.

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

Transpose Pandas DataFrame and change the column headers to a list

分類Dev

Transpose DF columns based on column values - Pandas

分類Dev

Transpose Range of a Column at a Time- Pandas

分類Dev

How to permute one column in pandas

分類Dev

pandas - two dateframes into one column

分類Dev

pandas - two dateframes into one column

分類Dev

Transpose Rows to Columns with Pandas

分類Dev

Python/Pandas transpose

分類Dev

Transpose whole dataframe into one row dataframe- (or transposing each row of data.table and column binding)

分類Dev

pandas count number of occurrences of values in one column

分類Dev

Sort dataframe by another on one column - pandas

分類Dev

Sort dataframe by another on one column - pandas

分類Dev

Pandas Dataframe filter rows by only one column

分類Dev

How to create pandas matrix from one column

分類Dev

Transpose specific rows into columns in pandas

分類Dev

pandas groupby with condition on one column to populate another column

分類Dev

How to take a portion of one column to match with a portion of another column in pandas?

分類Dev

Python pandas find element of one column in list of elements of another column

分類Dev

Pandas force one-to-one merge on column containing duplicate keys

分類Dev

Groupby and descendingly rank one column based on another one in Pandas

分類Dev

Pandas: creating a new column conditional on substring searches of one column and inverse of another column

分類Dev

Copy values from one column to another using Pandas

分類Dev

Updating the values of one column in a pandas dataframe based on input proportion

分類Dev

how to split a string in one column into new columns for each character in pandas

分類Dev

Combine multiple columns of pandas data frame to one column

分類Dev

Creating one hot encodings from a column of dictionaries with pandas

分類Dev

Pandas: Sort number of words in one column by the values of another

分類Dev

Use one column of a groupby to create X new columns with pandas

分類Dev

Pandas: How to calculate the percentage of one column against another?

Related 関連記事

  1. 1

    Transpose Pandas DataFrame and change the column headers to a list

  2. 2

    Transpose DF columns based on column values - Pandas

  3. 3

    Transpose Range of a Column at a Time- Pandas

  4. 4

    How to permute one column in pandas

  5. 5

    pandas - two dateframes into one column

  6. 6

    pandas - two dateframes into one column

  7. 7

    Transpose Rows to Columns with Pandas

  8. 8

    Python/Pandas transpose

  9. 9

    Transpose whole dataframe into one row dataframe- (or transposing each row of data.table and column binding)

  10. 10

    pandas count number of occurrences of values in one column

  11. 11

    Sort dataframe by another on one column - pandas

  12. 12

    Sort dataframe by another on one column - pandas

  13. 13

    Pandas Dataframe filter rows by only one column

  14. 14

    How to create pandas matrix from one column

  15. 15

    Transpose specific rows into columns in pandas

  16. 16

    pandas groupby with condition on one column to populate another column

  17. 17

    How to take a portion of one column to match with a portion of another column in pandas?

  18. 18

    Python pandas find element of one column in list of elements of another column

  19. 19

    Pandas force one-to-one merge on column containing duplicate keys

  20. 20

    Groupby and descendingly rank one column based on another one in Pandas

  21. 21

    Pandas: creating a new column conditional on substring searches of one column and inverse of another column

  22. 22

    Copy values from one column to another using Pandas

  23. 23

    Updating the values of one column in a pandas dataframe based on input proportion

  24. 24

    how to split a string in one column into new columns for each character in pandas

  25. 25

    Combine multiple columns of pandas data frame to one column

  26. 26

    Creating one hot encodings from a column of dictionaries with pandas

  27. 27

    Pandas: Sort number of words in one column by the values of another

  28. 28

    Use one column of a groupby to create X new columns with pandas

  29. 29

    Pandas: How to calculate the percentage of one column against another?

ホットタグ

アーカイブ