-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
[Feature Request] Add replace
method to Index
objects
#19495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You can do a type conversion. >>>idx = pd.Index([0,1,2])
...d = {1:100}
...idx = pd.DataFrame(idx).replace(d).set_index(0).index
...print(idx)
Int64Index([0, 100, 2], dtype='int64', name=0) |
Thanks @ZhuBaohe , there are a couple of work arounds but I think it would make sense to have a general method just like |
I've been using the following: replace = lambda x,d: d[x] if x in d else x
df.index.map(lambda x:replace(x, d_cpd_newcpdname)) Not sure if this is helpful at all. |
@jolespin is Index.rename what you want? |
@jorisvandenbossche I don't think it works the same way with >>> import pandas as pd
>>> idx = pd.Index([1,2,3])
>>> idx.rename({1:"a"})
Int64Index([1, 2, 3], dtype='int64', name={1: 'a'}) |
Sorry, I meant |
It's nice doing it to the actual index objects as well because I use them to store iterables of strings that I manipulate often. Is there a core method like the plans for to_csv for series and dataframes that could be applied to replace?
… On Feb 3, 2018, at 2:49 AM, Joris Van den Bossche ***@***.***> wrote:
Sorry, I meant DataFrame.rename(index={1: 'a'})
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Hmm, we could consider adding |
No objections for @jorisvandenbossche's suggestion? I "suffered" from this myself. Less dramatically, I expected that I could of course use a replace method on an index, so I'd like to give this a go if a PR is welcome. |
take |
I usually have to change the value of an element in
pd.Index
but it's difficult without thereplace
method that is present inpd.Series
. I usually have a bunch ofJupyter
cells depending on a particularpd.DataFrame
orpd.Series
with complicated transformations and a graph at some point. In most cases, I have to change the labels for a few to clean them up for publication quality figures. Currently, I have to use an if else statement withmap
method but it would nice to be able to just doreplace
.The text was updated successfully, but these errors were encountered: