我使用
创建数据框df3 = np.round(df2[["All"]]/df['Gender'].count()*100, 2).rename(columns={"All":'%'})
我希望在每个号码后添加%
。我该怎么做?
df
:
Third party unique identifier Qsex Gender
9ea3e3cb6719f3d336d324c446f486bd 1 male
d1b69bc4cccf0afef66debf4e3f0643e 2 female
f574fc585db0cddef88306ef6f32da59 1 male
8bc0a586bf0abec653c29cf4160753f9 1 male
7c22b56929378ec2eb3a536b4f4bc4e0 2 female
23d8433168c46d57a271a6b979037094 1 male
5743b7eec1b018572b6c5b44542a67a5 2 female
f176289325aa4a6fa56c0179e9cbd101 1 male
c729933ff7db798ae07c59d971f40a70 1 male
df2
Qsex 1.0 2.0 All
Gender
Female 0 72342 72342
Male 51537 0 51537
All 51537 72342 123879
答案 0 :(得分:1)
我认为您需要首先将normalize
添加到crosstab
,然后round
,将100
添加到多个,转换为string
并最后添加%
}:
df2 = pd.crosstab(df.Gender, df.Qsex, margins=True, normalize=True)
.round(2)
.mul(100)
.astype(str) + '%'
print (df2)
Qsex 1 2 All
Gender
female 0.0% 33.0% 33.0%
male 67.0% 0.0% 67.0%
All 67.0% 33.0% 100.0%
因为如果仅使用astype(str) + '%'
:
df2 = pd.crosstab(df.Gender, df.Qsex, margins=True).astype(str) + '%'
print (df2)
Qsex 1 2 All
Gender
female 0% 3% 3%
male 6% 0% 6%
All 6% 3% 9%