Pandas 带百分比计算的交叉表
Pandas Crosstab with Percentage calculation
我有一个 python 数据框,如下所示。我想crosstab/pivot用百分比计算如下。
owner = ['a','a','a','b','b','b','c','c','c']
user = ['a','a','a','b','b','b','c','c','c']
used_hours = [75,25,0,500,250,250,400,50,50]
available_hours = [100,100,100,1000,1000,1000,500,500,500]
data = {'Owner':owner,'User':user,'used_hours':used_hours,'available_hours':available_hours}
df = pd.DataFrame.from_dict(data)
当前输出:
df = pd.crosstab(df["Owner"], df["User"], normalize='index')
预期输出:
您可以在此处进行一些 post 格式化,但定义您自己的 to_percent
函数并聚合:
def to_percent(x):
return x * 100
print(pd.crosstab(df.Owner, df.User, values=(df.used_hours / df.available_hours), aggfunc=to_percent))
输出:
User a b c
Owner
a 75.0 25.0 0.0
b 50.0 25.0 25.0
c 80.0 10.0 10.0
我有一个 python 数据框,如下所示。我想crosstab/pivot用百分比计算如下。
owner = ['a','a','a','b','b','b','c','c','c']
user = ['a','a','a','b','b','b','c','c','c']
used_hours = [75,25,0,500,250,250,400,50,50]
available_hours = [100,100,100,1000,1000,1000,500,500,500]
data = {'Owner':owner,'User':user,'used_hours':used_hours,'available_hours':available_hours}
df = pd.DataFrame.from_dict(data)
当前输出:
df = pd.crosstab(df["Owner"], df["User"], normalize='index')
预期输出:
您可以在此处进行一些 post 格式化,但定义您自己的 to_percent
函数并聚合:
def to_percent(x):
return x * 100
print(pd.crosstab(df.Owner, df.User, values=(df.used_hours / df.available_hours), aggfunc=to_percent))
输出:
User a b c
Owner
a 75.0 25.0 0.0
b 50.0 25.0 25.0
c 80.0 10.0 10.0