问题
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