【问题标题】:pandas-change order of rows to make a value evenly distributedpandas-更改行的顺序以使值均匀分布
【发布时间】:2017-08-19 02:36:07
【问题描述】:

我有一个熊猫数据框:

A B C
-----
1 3 q 
2 2 q
3 6 q
4 7 q
5 8 q
3 8 q
1 3 s 
2 2 s
3 8 s

我希望带有“s”的行“均匀”分布在数据框中,如下所示:

A B C
-----
2 2 s
1 3 q 
2 2 q
3 6 q
3 8 s
4 7 q
5 8 q 
3 8 q
1 3 s

如何处理? 感谢您的帮助!

【问题讨论】:

  • 请试一试并展示您的尝试。

标签: python python-3.x pandas dataframe


【解决方案1】:

如果 s 的顺序不重要,那么试试这个?

A=len(df1)+len(df2)
df1.index=(list(range(0, A,4)))
df2.index=list(set(range(0, A))-set(range(0, A,4)))
df=pd.concat([df1,df2],axis=0).sort_index()


df
Out[147]: 
   A  B  C
0  1  3  s
1  1  3  q
2  2  2  q
3  3  6  q
4  2  2  s
5  4  7  q
6  5  8  q
7  3  8  q
8  3  8  s

【讨论】:

    【解决方案2】:

    保持每个“类别”中的行顺序:

    df1 = df[df.C == 'q']
    df2 = df[df.C == 's']
    
    spacing = int(len(df1) / (len(df2) - 1))
    df2ind = range(0, (len(df2) - 1) * (spacing + 1) + 1, spacing + 1)
    
    df1ind = [range(i + 1, i + 1 + spacing) for i in df2ind]
    df1ind = [i for l in df1ind for i in l][:len(df1)]
    
    df1.index = df1ind
    df2.index = df2ind
    df = pandas.concat([df1, df2]).sort_index()
    
    In: df
    Out: 
       A  B  C
    0  1  3  s
    1  1  3  q
    2  2  2  q
    3  3  6  q
    4  2  2  s
    5  4  7  q
    6  5  8  q
    7  3  8  q
    8  3  8  s
    

    【讨论】:

      猜你喜欢
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      相关资源
      最近更新 更多