如何用Pandas计算两个数据帧之间的百分比差异?
How to calculate percentage difference between two data frames with Pandas?
我正在使用 pandas
,我执行了一些计算和转换,最后我得到了两个数据框,大致如下所示:
ID 'abc' 'def'
Total 4 5
Slow 0 0
Normal 1 2
Fast 3 3
ID 'abc' 'def'
Total 3 4
Slow 0 0
Normal 0 1
Fast 3 3
现在,给定这两个数据框,我想生成第三个数据框,以某种方式 returns 第二个数据框满足第一个数据框的百分比。这样我希望结果是这样的:
ID 'abc' 'dfe'
Total 75.0% 80.0%
Slow None None
Normal 0.0% 50.0%
Fast 100.0% 100.0%
如果第一个数据框中有一个 0,那么在结果数据框中我们将该单元格设置为 None
或其他内容。整个想法是最后我将结果写入 Excel 文件,所以我希望 None
的单元格在 Excel 中为空。关于如何使用 pandas
在 Python 中执行此操作的任何想法?
您可以在感兴趣的列上简单地将 df2
除以 df1
:
df2.loc[:,"'abc'":] = df2.loc[:,"'abc'":].div(df1.loc[:,"'abc'":]).mul(100)
ID 'abc' 'dfe'
0 Total 75.0 80.0
1 Slow NaN NaN
2 Normal 0.0 50.0
3 Fast 100.0 100.0
更新
为了按照指定的格式进行格式化,您可以这样做:
df2.loc[:,"'abc'":] = df2.where(df2.loc[:,"'abc'":].isna(),
df2.round(2).astype(str).add('%'))
ID 'abc' 'dfe'
0 Total 75.0% 80.0%
1 Slow NaN NaN
2 Normal 0.0% 50.0%
3 Fast 100.0% 100.0%
鉴于没有小数位,除了.0
,round(2)
对显示的浮点数没有影响,但是一旦有一些浮点数在划分后有更多的小数位,您将看到所有浮点数的 2
小数位。
Pandas 提供了一些直接指定 styling in the output excel file 的可能性。它是有限的,但幸运的是,它包含一个数字格式选项。
import pandas as pd
# Initialize example dataframes
df1 = pd.DataFrame(
data=[[4, 5], [0, 0], [1, 2], [3, 3], [3, 3]],
index=['Total', 'Slow', 'Normal', 'Fast', 'Fast'],
columns=['abc', 'def'],
)
df2 = pd.DataFrame(
data=[[3, 4], [0, 0], [0, 1], [3, 3], [3, 3]],
index=['Total', 'Slow', 'Normal', 'Fast', 'Fast'],
columns=['abc', 'def'],
)
result_df = df2 / df1
# Change rows index into data column (to avoid any chance of having non-unique row index values,
# since the pandas styler can only handle unique row index)
result_df = result_df.reset_index()
# Write excel output file with number format styling applied
result_df.style.applymap(lambda _: 'number-format: 0.00%').to_excel('result.xlsx', engine='openpyxl', index=False)
我正在使用 pandas
,我执行了一些计算和转换,最后我得到了两个数据框,大致如下所示:
ID 'abc' 'def'
Total 4 5
Slow 0 0
Normal 1 2
Fast 3 3
ID 'abc' 'def'
Total 3 4
Slow 0 0
Normal 0 1
Fast 3 3
现在,给定这两个数据框,我想生成第三个数据框,以某种方式 returns 第二个数据框满足第一个数据框的百分比。这样我希望结果是这样的:
ID 'abc' 'dfe'
Total 75.0% 80.0%
Slow None None
Normal 0.0% 50.0%
Fast 100.0% 100.0%
如果第一个数据框中有一个 0,那么在结果数据框中我们将该单元格设置为 None
或其他内容。整个想法是最后我将结果写入 Excel 文件,所以我希望 None
的单元格在 Excel 中为空。关于如何使用 pandas
在 Python 中执行此操作的任何想法?
您可以在感兴趣的列上简单地将 df2
除以 df1
:
df2.loc[:,"'abc'":] = df2.loc[:,"'abc'":].div(df1.loc[:,"'abc'":]).mul(100)
ID 'abc' 'dfe'
0 Total 75.0 80.0
1 Slow NaN NaN
2 Normal 0.0 50.0
3 Fast 100.0 100.0
更新
为了按照指定的格式进行格式化,您可以这样做:
df2.loc[:,"'abc'":] = df2.where(df2.loc[:,"'abc'":].isna(),
df2.round(2).astype(str).add('%'))
ID 'abc' 'dfe'
0 Total 75.0% 80.0%
1 Slow NaN NaN
2 Normal 0.0% 50.0%
3 Fast 100.0% 100.0%
鉴于没有小数位,除了.0
,round(2)
对显示的浮点数没有影响,但是一旦有一些浮点数在划分后有更多的小数位,您将看到所有浮点数的 2
小数位。
Pandas 提供了一些直接指定 styling in the output excel file 的可能性。它是有限的,但幸运的是,它包含一个数字格式选项。
import pandas as pd
# Initialize example dataframes
df1 = pd.DataFrame(
data=[[4, 5], [0, 0], [1, 2], [3, 3], [3, 3]],
index=['Total', 'Slow', 'Normal', 'Fast', 'Fast'],
columns=['abc', 'def'],
)
df2 = pd.DataFrame(
data=[[3, 4], [0, 0], [0, 1], [3, 3], [3, 3]],
index=['Total', 'Slow', 'Normal', 'Fast', 'Fast'],
columns=['abc', 'def'],
)
result_df = df2 / df1
# Change rows index into data column (to avoid any chance of having non-unique row index values,
# since the pandas styler can only handle unique row index)
result_df = result_df.reset_index()
# Write excel output file with number format styling applied
result_df.style.applymap(lambda _: 'number-format: 0.00%').to_excel('result.xlsx', engine='openpyxl', index=False)