Home > Enterprise >  I need an idea how to write the dataframe horizontally, and no repeat the 'ProblemCode' co
I need an idea how to write the dataframe horizontally, and no repeat the 'ProblemCode' co

Time:09-22

currently:

ProblemGroup month Count
A 1 362
A 2 485
B 1 400
B 2 487

I need it this way:

month A B
1 362 400
2 485 487

CodePudding user response:

You can use pivot_table for that

import pandas as pd

data = [["A", 1, 362], ["A", 2, 485], ["B", 1, 400], ["B", 2, 487]]
df = pd.DataFrame(data, columns=["ProblemGroup", "Month", "Count"])
pd.pivot_table(df, values="Count", index="Month", columns="ProblemGroup")

outputs:

ProblemGroup    A    B
Month                 
1             362  400
2             485  487
  • Related