Multiple column with multiple columns in pandas

Martan

My data look like:

   A   B   C   Month
0  1   3   5    Jan
1  1   2   3    Feb

I need to: a) convert 'Month' to dummies

df2 = pd.get_dummies(df,columns=['Month'],drop_first=True,prefix = 'm')

b) Multiply A / B / C with all dummies generated. The only way I can think of doing this is

df_Feb = df2[['A','B','C']].multiply(df2['m_Feb], axis = "index")
df_March
...

and then join all newly created dataframe, which isn't very convenient. Is there is better way to approach this

jezrael

Idea is create MultiIndex in both DataFrames by MultiIndex.from_product and DataFrame.reindex, so possible multiple each other:

df1 = df[['A','B','C']]
df2 = pd.get_dummies(df['Month'])

mux = pd.MultiIndex.from_product([df1.columns, df2.columns])
df2 = df2.reindex(mux, axis=1, level=1)
df1 = df1.reindex(mux, axis=1, level=0)

df = df1 * df2

Last for correct ordering is used ordered CategoricalIndex and last flatten data columns with f-strings:

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", 
          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

df.columns = pd.MultiIndex.from_arrays([
    df.columns.get_level_values(0),
    pd.CategoricalIndex(df.columns.get_level_values(1),categories=months,ordered=True),
])
df = df.sort_index(axis=1)
df.columns = df.columns.map(lambda x: f'{x[0]}_{x[1]}')
print (df)

   A_Jan  A_Feb  B_Jan  B_Feb  C_Jan  C_Feb
0      1      0      3      0      5      0
1      0      1      0      2      0      3

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

Pandas Fillna of Multiple Columns with Mode of Each Column

分類Dev

pandas split column text into multiple columns

分類Dev

Pandas how to turn column of lists into multiple columns?

分類Dev

pandas dataframe column based on row and multiple columns

分類Dev

Apply (in Pandas) to Multiple Columns

分類Dev

pandas stack multiple columns into multiple columns

分類Dev

How best to extract a Pandas column containing lists or tuples into multiple columns

分類Dev

Combine multiple columns of pandas data frame to one column

分類Dev

Create Multiple New Columns Based on Pipe-Delimited Column in Pandas

分類Dev

Extracting portions of JSON string column containing multiple rows and columns in pandas

分類Dev

Pandas groupby multiple columns basis date column by epoch week

分類Dev

How to find the average of multiple columns using a common column in pandas

分類Dev

How to expand a pandas column with a list of dictionaries into multiple columns

分類Dev

Merge multiple columns into one column with multiple rows

分類Dev

Split column values into multiple columns with

分類Dev

Multiple columns with the same name in Pandas

分類Dev

Pandas Groupby、MultiIndex、Multiple Columns

分類Dev

Pandas Multiply multiple columns in loop

分類Dev

Vlookup in multiple columns at Python (pandas)

分類Dev

pandas MultiIndex assign multiple columns

分類Dev

pandas hierarchical group by multiple columns

分類Dev

Convert Multiple pandas Column into json

分類Dev

Pandas DataFrame apply function to multiple columns and output multiple columns

分類Dev

Pandas: Sum multiple columns, but write NaN if any column in that row is NaN or 0

分類Dev

Pandas/Python: interpolation of multiple columns based on values specified for one reference column

分類Dev

How do I merge pandas df with multiple columns using one key from another column?

分類Dev

Pandas: Group values of multiple columns according to categories in a different column. And calculate avg based on the grouping for that category

分類Dev

How to merge multiple columns values into one column?

分類Dev

Split a column into multiple binary dummy columns

Related 関連記事

  1. 1

    Pandas Fillna of Multiple Columns with Mode of Each Column

  2. 2

    pandas split column text into multiple columns

  3. 3

    Pandas how to turn column of lists into multiple columns?

  4. 4

    pandas dataframe column based on row and multiple columns

  5. 5

    Apply (in Pandas) to Multiple Columns

  6. 6

    pandas stack multiple columns into multiple columns

  7. 7

    How best to extract a Pandas column containing lists or tuples into multiple columns

  8. 8

    Combine multiple columns of pandas data frame to one column

  9. 9

    Create Multiple New Columns Based on Pipe-Delimited Column in Pandas

  10. 10

    Extracting portions of JSON string column containing multiple rows and columns in pandas

  11. 11

    Pandas groupby multiple columns basis date column by epoch week

  12. 12

    How to find the average of multiple columns using a common column in pandas

  13. 13

    How to expand a pandas column with a list of dictionaries into multiple columns

  14. 14

    Merge multiple columns into one column with multiple rows

  15. 15

    Split column values into multiple columns with

  16. 16

    Multiple columns with the same name in Pandas

  17. 17

    Pandas Groupby、MultiIndex、Multiple Columns

  18. 18

    Pandas Multiply multiple columns in loop

  19. 19

    Vlookup in multiple columns at Python (pandas)

  20. 20

    pandas MultiIndex assign multiple columns

  21. 21

    pandas hierarchical group by multiple columns

  22. 22

    Convert Multiple pandas Column into json

  23. 23

    Pandas DataFrame apply function to multiple columns and output multiple columns

  24. 24

    Pandas: Sum multiple columns, but write NaN if any column in that row is NaN or 0

  25. 25

    Pandas/Python: interpolation of multiple columns based on values specified for one reference column

  26. 26

    How do I merge pandas df with multiple columns using one key from another column?

  27. 27

    Pandas: Group values of multiple columns according to categories in a different column. And calculate avg based on the grouping for that category

  28. 28

    How to merge multiple columns values into one column?

  29. 29

    Split a column into multiple binary dummy columns

ホットタグ

アーカイブ