Reputation: 4388
I have a dataframe, df, where I wish to have the percent and type displayed. The code works well except when I type df.columns , both columns (Type and Percent) are not listed.
Data
Type
Hello-HEL-HE-A6123-123A-12T_TYPE-v.A
Hello-HEL-HE-A6123-123A-12T_TYPE-v.B
Hello-HEL-HE-A6123-123A-12T_TYPE-v.E
Hello-HEL-HE-A6123-123A-50T_TYPE-v.C
Hello-HEL-HE-A6123-123A-50T_TYPE-v.A
Happy-HAP-HA-R650-570A-90T_version-v.A
Desired
Type Percent
Hello-HEL-HE-12T 50%
Hello-HEL-HE-50T 33%
Happy-HAP-HA-90T 16.6%
Doing
df.columns = ['Type']
df['computer.u_sku'] = df['computer.u_sku'].apply(lambda x: extract_value(x))
df1 = (df['Type'].value_counts(normalize=True)*100).to_frame('Percent')
print(df1.rename_axis(index='Type').reset_index())
Outcome
Type Percent
Hello-HEL-HE-12T 50%
Hello-HEL-HE-50T 33%
Happy-HAP-HA-90T 16.6%
However, when I type in:
df.columns , the output only lists Percent as a column. How would I have the code display both columns. I am thinking this may have something to do with setting the index. I am researching this. Any suggestion is appreciated.
Index(['Percent'], dtype='object')
I have tried this:
df2.columns = ['Type', 'Percent'], but I get a length mismatch error
Upvotes: 0
Views: 188