weixin_44368101
pomato_9595
采纳率0%
2019-01-09 19:21

python中 dataframe使用permutation和take对 列 进行随机排序失败

请问python中 dataframe使用sampler和take对列进行随机排列为什么不行呢?

import pandas as pd
import numpy as np
from pandas import DataFrame
from pandas import Series
df=DataFrame(np.arange(20).reshape(5,4))
sampler=np.random.permutation(5)
print(df.take(sampler,axis=1))

报错如下
Traceback (most recent call last):
File "C:\Users\XXXX\Desktop\test190109.py", line 7, in
print(df.take(sampler,axis=1))
File "C:\Users\XXXxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\generic.py", line 2891, in take
return self._take(indices, axis=axis, is_copy=is_copy)
File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\generic.py", line 2789, in _take
verify=True)
File "C:\Users\XXXXXX\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\internals.py", line 4530, in take
indexer = maybe_convert_indices(indexer, n)
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 2480, in maybe_convert_indices
raise IndexError("indices are out-of-bounds")
IndexError: indices are out-of-bounds

目测是最后一行axis=1的问题?去掉了就能跑了

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • weixin_39416561 lyhsdy 2年前

    ←如果以下回答对你有帮助,请点击右边的向上箭头及采纳下答案

    因为你只有4列,所以改成sampler=np.random.permutation(4)就可以了

    import pandas as pd
    import numpy as np
    from pandas import DataFrame
    from pandas import Series
    df=DataFrame(np.arange(20).reshape(5,4))
    sampler=np.random.permutation(4)
    print(df.take(sampler,axis=1))
    
    
    点赞 评论 复制链接分享

相关推荐