1

folllowing this thread: Renaming columns in pandas. I want to use this line:

df.columns = df.columns.str.replace('$','')

The problem is I pivoted the dataframe with df.pivot_table().to_records()and converted it to records, now my column headers have the following syntax: "('content2', ':13A::STAT')"

from this I only want to keep: 13A::STAT, any idea to do so?

Is there any way to avoid this renaming in pivoting the dataframe?

Example dataframe (after pivoting):

occurance ('content2, ':13A::STAT') ('content2', '17B::ACTI')
1          4000                       Y
2            NaN                       73000

Expected output:

occurance 13A::STAT                  17B::ACTI
1          4000                       Y
2            NaN                       73000
PV8
  • 5,799
  • 7
  • 43
  • 87
  • `df1 = pd.DataFrame(df.pivot_table(index='occurance', columns="random", values=['content2'], aggfunc = lambda x: x.tolist() if len(list(x)) > 1 else x.iat[0]).to_records())` – PV8 Jun 24 '19 at 06:31

1 Answers1

2

You can omit MultiIndex in column if omit [] in ['content2']:

df1 = (df.pivot_table(index='occurance', 
                     columns="random", 
                     values='content2', 
                     aggfunc = lambda x: x.tolist() if len(list(x)) > 1 else x.iat[0])
        .to_records())
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252