0

Is there a python pandas method to rename contents of a DataFrame using dictionaries or otherwise?

I normally use rename method on index labels and column titles. But when dataframe entries are to be replaced using dictionaries I use set_index to make them index, then use rename method on index and later reset_index. I understand this is round-about.

Is there any other way this can be done using fewer lines of code?

example: Suppose below is my dataframe & I want to change acronyms under column title State with State Names i.w. CA - California, IL - Illinois etc

Rank    State   Metro                           CountyName
1       NY      New York                        Queens
2       CA      Los Angeles-Long Beach-Anaheim  Los Angeles
3       IL      Chicago                         Cook
4       PA      Philadelphia                    Philadelphia
5       AZ      Phoenix                         Maricopa
6       NV      Las Vegas                       Clark
7       CA      San Diego                       San Diego
8       TX      Dallas-Fort Worth               Dallas
9       CA      San Jose                        Santa Clara
Vaishali
  • 37,545
  • 5
  • 58
  • 86
Krishna
  • 41
  • 6
  • It's a duplicate, so not posting as an answer but you can directly replace the contents of a column using df['State] = df.State.replace({'CA': 'California'}) – Vaishali Nov 22 '17 at 14:50
  • Thanks Vaishaliji. That worked perfectly as I expected. – Krishna Nov 22 '17 at 14:59

0 Answers0