Change one value based on another value in pandas

I'm trying to reprogram my Stata code into Python for speed improvements, and I was pointed in the direction of PANDAS. I am, however, having a hard time wrapping my head around how to process the data.

Let's say I want to iterate over all values in the column head 'ID.' If that ID matches a specific number, then I want to change two corresponding values FirstName and LastName.

In Stata it looks like this:

    replace FirstName = "Matt" if ID==103
    replace LastName =  "Jones" if ID==103

So this replaces all values in FirstName that correspond with values of ID == 103 to Matt.

In PANDAS, I'm trying something like this

    df = read_csv("test.csv")
    for i in df['ID']:
        if i ==103:
              ...

Not sure where to go from here. Any ideas?

One option is to use Python's slicing and indexing features to logically evaluate the places where your condition holds and overwrite the data there.

Assuming you can load your data directly into pandas with pandas.read_csv then the following code might be helpful for you.

    import pandas
    df = pandas.read_csv("test.csv")
    df.loc[df.ID == 103, 'FirstName'] = "Matt"
    df.loc[df.ID == 103, 'LastName'] = "Jones"

As mentioned in the comments, you can also do the assignment to both columns in one shot:

    df.loc[df.ID == 103, ['FirstName', 'LastName']] = 'Matt', 'Jones'

Note that you'll need pandas version 0.11 or newer to make use of loc for overwrite assignment operations.

Another way to do it is to use what is called chained assignment. The behavior of this is less stable and so it is not considered the best solution (it is explicitly discouraged in the docs), but it is useful to know about:

    import pandas
    df = pandas.read_csv("test.csv")
    df['FirstName'][df.ID == 103] = "Matt"
    df['LastName'][df.ID == 103] = "Jones"

From: stackoverflow.com/q/19226488

Back to homepage or read more recommendations: