我有数据:
Third party unique identifier Qsex Qage Qfamilystatus QeducationSingle Qincomeevaluation Qjobstatus QRuCitySize QRuDistrict Qcountry
9ea3e3cb6719f3d336d324c446f486bd 1 32 1 5 1 1 1 1
cb570bb986808a5f4d2629287297b902 2 25 5 2 1 1 1
78b3a44eb7c7f7c687ffbcfed57647a4 1 30 4 1 3 6 1
1c728b223a4c2c267f3a3630b4a63f6e 2 45 4 1 1 1 1
8852ecd198fddfa557186c863f2c6fdf 2 41 4 1 7 7 1
1adc146b9ec35f7c632902f480d7e95c 1 70 5 3 1 1 1
0fb0c903a6b2b68f1b0a7cd1962f353c 1 29 5 1 5 7 1
另一个df:
QRuDistrict 1 ЦФО
QRuDistrict 2 ЮФО
QRuDistrict 3 СЗФО
QRuDistrict 4 ДВФО
QRuDistrict 5 СФО
QRuDistrict 6 УФО
QRuDistrict 7 ПФО
QRuDistrict 8 СКФО
QRuDistrict 9 Крымский ФО
我尝试将第一个df中的值替换为第二个df中的数据,然后计算百分比并将其写入excel
。你知道吗
我使用:
d = (df_1[df_1['sign']=='Qcountry'].set_index('number')['result'].to_dict())
df['Country'] = df.Qcountry.map(d)
df2 = pd.crosstab(df.Country, df.Qcountry, margins=True)
df3 = np.round(df2[["All"]] / df['Country'].count() * 100, 2).rename(columns={"All": '%'})
country = pd.concat([df2[["All"]], df3], axis=1)
less = country[country['%'] < 5]
country = country[country['%'] >= 5]
country['All'] = ((all_users * df3.divide(100)).astype(int))
country['%'] = country['%'].astype(str) + '%'
country.to_excel(writer, sheet_name=sheet_name, startrow=48, startcol=4)
获得:
Federal Districts Россия
N %
ДВФО 131 5.33%
Крымский ФО 11 0.48%
ПФО 416 16.91%
СЗФО 420 17.09%
СКФО 43 1.75%
СФО 259 10.53%
УФО 208 8.48%
ЦФО 764 31.08%
ЮФО 205 8.35%
Total 2461 100.0%
但我想得到第二个数据帧中的序列。 我想得到:
Federal Districts Россия
N %
ЦФО 764 31.08%
ЮФО 205 8.35%
СЗФО 420 17.09%
ДВФО 131 5.33%
СФО 259 10.53%
УФО 208 8.48%
ПФО 416 16.91%
СКФО 43 1.75%
Крымский ФО 11 0.48%
Total 2461 100.0%
我怎样才能按这个顺序排序呢?你知道吗
我认为您可以按第二个数据帧使用^{} ,但有必要将最后一项
['Total']
添加到list
:如果使用^{} ,则顺序不同:
按注释编辑:
我更改了列名,似乎只需要列} 与^{} 一起使用,并将^{} 一起使用掩码:
sign
的值,其中第一列number
包含QRuDistrict
。然后可以将^{相关问题 更多 >
编程相关推荐