简体   繁体   中英

Can we alter pandas cross tabulation?

I have loaded raw_data from MySQL using sqlalchemy and pymysql

engine = create_engine('mysql+pymysql://[user]:[passwd]@[host]:[port]/[database]')

df = pd.read_sql_table('data', engine)

df is something like this

| Age Category | Category       |
|--------------|----------------|
| 31-26        | Engaged        |
| 26-31        | Engaged        |
| 31-36        | Not Engaged    |
| Above 51     | Engaged        |
| 41-46        | Disengaged     |
| 46-51        | Nearly Engaged |
| 26-31        | Disengaged     |

Then i had performed analysis as follow

age = pd.crosstab(df['Age Category'], df['Category'])

| Category     | A | B  | C  | D |
|--------------|---|----|----|---|
| Age Category |   |    |    |   |
| 21-26        | 2 | 2  | 4  | 1 |
| 26-31        | 7 | 11 | 12 | 5 |
| 31-36        | 3 | 5  | 5  | 2 |
| 36-41        | 2 | 4  | 1  | 7 |
| 41-46        | 0 | 1  | 3  | 2 |
| 46-51        | 0 | 0  | 2  | 3 |
| Above 51     | 0 | 3  | 0  | 6 |

I want to change it to Pandas DataFrame something like this.

| Age Category | A | B  | C  | D |
|--------------|---|----|----|---|
| 21-26        | 2 | 2  | 4  | 1 |
| 26-31        | 7 | 11 | 12 | 5 |
| 31-36        | 3 | 5  | 5  | 2 |
| 36-41        | 2 | 4  | 1  | 7 |
| 41-46        | 0 | 1  | 3  | 2 |
| 46-51        | 0 | 0  | 2  | 3 |
| Above 51     | 0 | 3  | 0  | 6 |

Thank you for your time and consideration

Both texts are called columns and index names, solution for change them is use DataFrame.rename_axis :

age = age.rename_axis(index=None, columns='Age Category')

Or set columns names by index names, and then set index names to default - None :

age.columns.name = age.index.name
age.index.name = None

print (age)
Age Category  Disengaged  Engaged  Nearly Engaged  Not Engaged
26-31                  1        1               0            0
31-26                  0        1               0            0
31-36                  0        0               0            1
41-46                  1        0               0            0
46-51                  0        0               1            0
Above 51               0        1               0            0

But this texts are something like metadata, so some functions should remove them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM