我正在尝试生成报告,然后运行以下代码
import pandas as pd
df = pd.read_excel("proposals2020.xlsx", sheet_name="Proposals")
country_probability = df.groupby(["Country", "Probability"]).count()
country_probability = country_probability.unstack()
country_probability = country_probability.fillna("0")
country_probability = country_probability.drop(country_probability.columns[4:], axis=1)
country_probability = country_probability.drop(country_probability.columns[0], axis=1)
country_probability = country_probability.astype(int)
print(country_probability)
我得到以下结果:
Quote Number
Probability High Low Medium
Country
Algeria 3 1 9
Bahrain 4 3 2
Egypt 2 0 3
Iraq 3 0 8
Jordan 0 1 1
Lebanon 0 1 0
Libya 1 0 0
Morocco 0 0 2
Pakistan 3 10 11
Qatar 0 1 1
Saudi Arabia 16 8 19
Tunisia 2 5 0
USA 0 1 0
我的问题是如何阻止熊猫按字母顺序对这些列进行排序并保持高,中,低顺序...
答案 0 :(得分:1)
DataFrame.reindex
@guvectorize(['float64[:,:,:],float64[:,:,:,:],float64[:,:,:]'],'(l,n,m),(k,l,n,m)->(l,n,m)', target='cuda')
def lstsq(a,b,c):
k,l,n,m = b.shape
for i in range(n):
for j in range(m):
c[:,i,j] = cp.linalg.lstsq(b[:,:,i,j],a[:,i,j])[0]
如果列中没有MultiIndex:
# if isinstance(df.columns, pd.MultiIndex)
df = df.reindex(['High', 'Medium', 'Low'], axis=1, level=1)
我们还可以尝试在# if isinstance(df.columns, pd.Index)
df = df.reindex(['High', 'Medium', 'Low'], axis=1)
中传递 sort = False :
groupby