How to rename the index of a Dask Dataframe

爱⌒轻易说出口 提交于 2019-12-07 03:53:59

问题


How would I go about renaming the index on a dask dataframe? I tried it like so

df.index.name = 'foo'

but rechecking df.index.name shows it still being whatever it was previously.


回答1:


This does not seem like an efficient way to do it, so I wouldn't be surprised if there is something more direct.

d.index.name starts off as 'foo';

def f(df, name):
    df.index.name = name
    return df

d.map_partitions(f, 'pow')

The output now has index name of 'pow'. If this is done with the threaded scheduler, I think you also change the index name of d in-place (in which case you don't really need the output of map_partitions).




回答2:


what you are doing should be working, check again :

    df = pd.DataFrame(np.random.rand(2,2))
    df
    Out[7]: 
              0         1
    0  0.016233  0.856702
    1  0.597350  0.710255

df.index.name = 'foo'
df
Out[10]: 
            0         1
foo                    
0    0.016233  0.856702
1    0.597350  0.710255

df.index
Out[11]: 
RangeIndex(start=0, stop=2, step=1, name='foo')



# even renaming after :
df.index.name ='foo2'
df
Out[14]: 
             0         1
foo2                    
0     0.016233  0.856702
1     0.597350  0.710255


来源:https://stackoverflow.com/questions/44337910/how-to-rename-the-index-of-a-dask-dataframe

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