how to re order a pandas dataframe based on a dictionary condition

可紊 提交于 2019-12-11 17:03:16

问题


I have a df like this,

    case        step    deep                   value
0   case 1      1       ram in India           ram,cricket
1   NaN         2       ram plays cricket       NaN
2   case 2      1       ravi played football   ravi
3   NaN         2       ravi works welll        NaN
4   case 3      1       Sri bought a car       sri
5   NaN         2       sri went out            NaN

and a dictionary, my_dict = {ram:1,cricket:1,ravi:2.5,sri:1}

I am trying to re-order the dataframe according to the values of the dictionary, I achieved this dictionary using tfidf method. I face difficulty in re-ordering as we need to re-order the rows including with the values.

My expected output is,

    case        step    deep                   value
2   case 2      1       ravi played football   ravi
3   NaN         2       ravi works welll        NaN
0   case 1      1       ram in India           ram,cricket
1   NaN         2       ram plays cricket       NaN
4   case 3      1       Sri bought a car       sri
5   NaN         2       sri went out            NaN

Please help, thanks in advance!


回答1:


You can create MultiIndex for sorting, only is necessary values from column value are in my_dict:

my_dict = {'ram':1,'cricket':1,'ravi':2.5,'sri':1}

#create DataFrame from value column, replace and sum columns
a = df['value'].str.split(',', expand=True).replace(my_dict).sum(axis=1)
#create groups
b = df['step'].diff().le(0).cumsum()
#create Series by summing per groups
c = a.groupby(b).transform('sum')
#create MultiIndex
df.index = [c,b]
print (df)
            case  step                  deep        value
    step                                                 
2.0 0     case 1     1          ram in India  ram,cricket
    0        NaN     2     ram plays cricket          NaN
2.5 1     case 2     1  ravi played football         ravi
    1        NaN     2      ravi works welll          NaN
1.0 2     case 3     1      Sri bought a car          sri
    2        NaN     2          sri went out          NaN

#sorting MultiIndex and removing
df = df.sort_index(ascending=False).reset_index(drop=True)
print (df)
     case  step                  deep        value
0  case 2     1  ravi played football         ravi
1     NaN     2      ravi works welll          NaN
2  case 1     1          ram in India  ram,cricket
3     NaN     2     ram plays cricket          NaN
4  case 3     1      Sri bought a car          sri
5     NaN     2          sri went out          NaN


来源:https://stackoverflow.com/questions/48860471/how-to-re-order-a-pandas-dataframe-based-on-a-dictionary-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!