Pandas 如何将未加支撑的熊猫数据框重塑为;“长”;表单,然后将其传递给绘图函数

Pandas 如何将未加支撑的熊猫数据框重塑为;“长”;表单,然后将其传递给绘图函数,pandas,plotly,plotly-python,Pandas,Plotly,Plotly Python,我试图使用Plotlypx.bar()函数制作一个显示比率的简单条形图 我有以下数据集: test_df = pd.DataFrame({'Manufacturer':['Ford', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'B

我试图使用Plotly
px.bar()
函数制作一个显示比率的简单条形图

我有以下数据集:

test_df = pd.DataFrame({'Manufacturer':['Ford', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW'],
                          'Metric':['Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Sales', 'Sales', 'Sales', 'Sales', 'Sales', 'Sales', 'Warranty', 'Warranty', 'Warranty', 'Warranty', 'Warranty', 'Warranty'],
                          'Sector':['Germany', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA'],
                          'Value':[45000, 70000, 90000, 65000, 40000, 65000, 63000, 2700, 4400, 3400, 3000, 4700, 5700, 1500, 2000, 2500, 1300, 2000, 2450],
                          'City': ['Frankfurt', 'Bremen', 'Berlin', 'Hamburg', 'New York', 'Chicago', 'Los Angeles', 'Dresden', 'Munich', 'Cologne', 'Miami', 'Atlanta', 'Phoenix', 'Nuremberg', 'Dusseldorf', 'Leipzig', 'Houston', 'San Diego', 'San Francisco']
                   })
我重置了索引并创建了一个透视表,如下所示:

temp_table = test_df.reset_index().pivot_table(values = 'Value', index = ['Manufacturer', 'Metric', 'Sector'], aggfunc='sum')
然后,我创建两个新数据帧:

s1 = temp_table.set_index(['Manufacturer','Sector']).query("Metric=='Orders'").Value
s2 = temp_table.set_index(['Manufacturer','Sector']).query("Metric=='Sales'").Value
s1.div(s2).unstack()
然后,我取消堆叠这些数据帧:

s1 = temp_table.set_index(['Manufacturer','Sector']).query("Metric=='Orders'").Value
s2 = temp_table.set_index(['Manufacturer','Sector']).query("Metric=='Sales'").Value
s1.div(s2).unstack()
这给了我:

Sector             Germany      USA
Manufacturer
---
BMW               19.117647     11.052632
Ford              42.592593     13.333333
Mercedes          20.454545     13.829787
我希望能够使用上述数据绘制条形图,x轴上有
制造商
,并用
扇区
着色,如下所示:

为此,我认为我需要以下长格式的数据:

Manufacturer       Sector        Ratio
BMW                Germany       19.117647
Ford               Germany       42.592593
Mercedes           Germany       20.454545
BMW                USA           11.052632
Ford               USA           13.333333
Mercedes           USA           13.829787
问题:如何重塑上面的未堆叠数据,以便能够将其传递给Plotly px.bar()函数,该函数要求x轴和y轴参数满足以下条件:

x(str或int或Series或array-like)–数据帧中的列名称,或pandas-Series或array-like对象。此列或类似数组的值用于在笛卡尔坐标系中沿x轴定位标记。x或y可以是列引用或数组类的列表,在这种情况下,数据将被视为“宽”而不是“长”


提前谢谢

只是不要取消堆叠

df_out=s1.div(s2).reset_index()

只是不要取消堆叠

df_out=s1.div(s2).reset_index()

这会给你上面的条形图


test_df.groupby(['Manufacturer','Sector'])['Value'].sum().unstack('Sector').plot.bar()

这将为您提供上面的条形图


test_-df.groupby(['Manufacturer',Sector'])['Value'].sum().unstack('Sector').plot.bar()
test_-df.groupby(['Manufacturer',Sector'])你能试试
test_-df.groupby(['Manufacturer',Sector'])['Value']吗
我可以使用.plot.bar()函数。。。但是,我想使用Plotly函数,它需要重塑。我可以使用.plot.bar()函数。。。但是,我想使用Plotly函数,它需要重塑。