如何使用 python 将数据框中的列从小数转换为百分比
How to convert a column in a dataframe from decimals to percentages with python
我已经尝试了很长时间,但无论我做什么,我都无法将此列从小数转换为百分比。这是我的数据框
Groups and Classes Booked Arrived No Show (n) No Show (%)
6 Supervised Exercise Program 121 57 64 5e-01
0 Active Living 101 4 2 2 5e-01
2 Eating Well the Mediterranean Way 38 24 14 4e-01
5 MBCT 101 66 35 3e-01
4 Healthy Meal Planning 33 22 11 3e-01
3 Grief 56 46 10 2e-01
1 Craving Change 115 95 20 2e-01
Total NaN 468 312 156 3e-01
我想转换我的“未显示 (%)”列,以便它输出如下百分比:
Groups and Classes Booked Arrived No Show (n) No Show (%)
6 Supervised Exercise Program 121 57 64 53%
0 Active Living 101 4 2 2 50%
2 Eating Well the Mediterranean Way 38 24 14 37%
5 MBCT 101 66 35 35%
4 Healthy Meal Planning 33 22 11 33%
3 Grief 56 46 10 18%
1 Craving Change 115 95 20 17%
Total NaN 468 312 156 33%
我已经尝试了以下方法并且它运行了,但它不会将我的列转换为百分比:
mergedgc = mergedgc.sort_values('No Show (%)', ascending=False)
mergedgc.loc['Total'] = mergedgc.sum(numeric_only=True)
pd.set_option('precision', 0)
mergedgc['No Show (%)'] = round((mergedgc['No Show (n)'] / mergedgc['Booked']),2)
mergedgc.style.format({"No Show (%)": "{:.2%}"})
有人可以告诉我我做错了什么吗?我不认为将列转换为百分比会如此令人头疼。
玩具示例:
df=DataFrame({
'No Show (%)':[5e-01, 4e-01]
})
df
输入
No Show (%)
0 0.5
1 0.4
代码
mergedgc.style.format({"No Show (%)": "{:.2%}"})
可以替换为
df['No Show (%)'] = df['No Show (%)'].transform(lambda x: '{:,.2%}'.format(x))
输出
No Show (%)
0 50.00%
1 40.00%
编辑
情节
df['No Show (%)'].replace('\%','', regex=True).astype(float).plot()
你可以试试这个:
df["No Show (%)"] = pd.Series(
[f"{value}%" for value in round(df["No show (%)"] * 100, 0)]
)
print(df["No show"])
#Outputs
No Show (%)
6 53.0%
0 50.0%
2 37.0%
试试这个:
df["No_show_percentage"] = ((df["NoShow"]/df["Booked"])*100).round(2)
df```
Result:
Booked NoShow No_show_percentage
0 121 64 52.89
1 4 2 50.00
可以使用.map()
配合lambda函数输出f-string进行格式化,如下:
df['No Show (%)'] = df['No Show (%)'].map(lambda x: f'{x:,.0%}', na_action='ignore')
参数na_action='ignore'
用于避免转换NaN值(如果有的话)。
演示
df['No Show (%)'] = df['No Show (n)'] / df['Booked']
df['No Show (%)'] = df['No Show (%)'].map(lambda x: f'{x:,.0%}', na_action='ignore')
print(df)
Groups and Classes Booked Arrived No Show (n) No Show (%)
6 Supervised Exercise Program 121 57 64 53%
0 Active Living 101 4 2 2 50%
2 Eating Well the Mediterranean Way 38 24 14 37%
5 MBCT 101 66 35 35%
4 Healthy Meal Planning 33 22 11 33%
3 Grief 56 46 10 18%
1 Craving Change 115 95 20 17%
Total NaN 468 312 156 33%
如果您希望百分比有 2 个小数点(从您尝试过的代码中可以看出这与您的示例预期输出不同),您可以将 f 字符串更改为:f'{x:,.2%}'
我已经尝试了很长时间,但无论我做什么,我都无法将此列从小数转换为百分比。这是我的数据框
Groups and Classes Booked Arrived No Show (n) No Show (%)
6 Supervised Exercise Program 121 57 64 5e-01
0 Active Living 101 4 2 2 5e-01
2 Eating Well the Mediterranean Way 38 24 14 4e-01
5 MBCT 101 66 35 3e-01
4 Healthy Meal Planning 33 22 11 3e-01
3 Grief 56 46 10 2e-01
1 Craving Change 115 95 20 2e-01
Total NaN 468 312 156 3e-01
我想转换我的“未显示 (%)”列,以便它输出如下百分比:
Groups and Classes Booked Arrived No Show (n) No Show (%)
6 Supervised Exercise Program 121 57 64 53%
0 Active Living 101 4 2 2 50%
2 Eating Well the Mediterranean Way 38 24 14 37%
5 MBCT 101 66 35 35%
4 Healthy Meal Planning 33 22 11 33%
3 Grief 56 46 10 18%
1 Craving Change 115 95 20 17%
Total NaN 468 312 156 33%
我已经尝试了以下方法并且它运行了,但它不会将我的列转换为百分比:
mergedgc = mergedgc.sort_values('No Show (%)', ascending=False)
mergedgc.loc['Total'] = mergedgc.sum(numeric_only=True)
pd.set_option('precision', 0)
mergedgc['No Show (%)'] = round((mergedgc['No Show (n)'] / mergedgc['Booked']),2)
mergedgc.style.format({"No Show (%)": "{:.2%}"})
有人可以告诉我我做错了什么吗?我不认为将列转换为百分比会如此令人头疼。
玩具示例:
df=DataFrame({
'No Show (%)':[5e-01, 4e-01]
})
df
输入
No Show (%)
0 0.5
1 0.4
代码
mergedgc.style.format({"No Show (%)": "{:.2%}"})
可以替换为
df['No Show (%)'] = df['No Show (%)'].transform(lambda x: '{:,.2%}'.format(x))
输出
No Show (%)
0 50.00%
1 40.00%
编辑
情节
df['No Show (%)'].replace('\%','', regex=True).astype(float).plot()
你可以试试这个:
df["No Show (%)"] = pd.Series(
[f"{value}%" for value in round(df["No show (%)"] * 100, 0)]
)
print(df["No show"])
#Outputs
No Show (%)
6 53.0%
0 50.0%
2 37.0%
试试这个:
df["No_show_percentage"] = ((df["NoShow"]/df["Booked"])*100).round(2)
df```
Result:
Booked NoShow No_show_percentage
0 121 64 52.89
1 4 2 50.00
可以使用.map()
配合lambda函数输出f-string进行格式化,如下:
df['No Show (%)'] = df['No Show (%)'].map(lambda x: f'{x:,.0%}', na_action='ignore')
参数na_action='ignore'
用于避免转换NaN值(如果有的话)。
演示
df['No Show (%)'] = df['No Show (n)'] / df['Booked']
df['No Show (%)'] = df['No Show (%)'].map(lambda x: f'{x:,.0%}', na_action='ignore')
print(df)
Groups and Classes Booked Arrived No Show (n) No Show (%)
6 Supervised Exercise Program 121 57 64 53%
0 Active Living 101 4 2 2 50%
2 Eating Well the Mediterranean Way 38 24 14 37%
5 MBCT 101 66 35 35%
4 Healthy Meal Planning 33 22 11 33%
3 Grief 56 46 10 18%
1 Craving Change 115 95 20 17%
Total NaN 468 312 156 33%
如果您希望百分比有 2 个小数点(从您尝试过的代码中可以看出这与您的示例预期输出不同),您可以将 f 字符串更改为:f'{x:,.2%}'