[Tutor] How would I replace the data of a Pandas Series with the values of a dictionary?
Albert-Jan Roskam
sjeik_appie at hotmail.com
Thu Jul 18 05:24:05 EDT 2019
On 16 Jul 2019 23:31, Daniel Bosah <dbosah at buffalo.edu> wrote:
Hi all,
I have a problem trying to match items in a dict and pandas series in
Python.
I have a dict ( called city_dict )of cities and city_id's ; for each city (
which is a key in the dict ), a unique city_id is a value in that dict.
So for example, city_dict = { New York : 1001, LA : 1002, Chicago : 1003 }.
New York is a key, 1001 is a value.
Now I have a panda Series called dfCities. In this series is a bunch of
cities, including the cities in city_dict.
My goal is to replace the cities in dfCities with the city_id's in a brand
new csv file. So if dfCities has New York in it, I want to replace it with
it's value in the dictionary, so 1001.
=====》 check out https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.replace.html. It accepts a dict, see also examples below.
More information about the Tutor
mailing list