187

I'm trying to reproduce my Stata code in Python, 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?

7 Answers 7

314

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. Indeed, for older versions like 0.8 (despite what critics of chained assignment may say), chained assignment is the correct way to do it, hence why it's useful to know about even if it should be avoided in more modern versions of pandas.


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"
11
  • 22
    how about adding also this flavor: df.loc[df.ID == 103, ['FirstName', 'LastName']] = 'Matt', 'Jones'
    – Zeugma
    Oct 7, 2013 at 13:54
  • 5
    -1 "Another way to do it is to use what is called chained assignment." No. Emphatically, no. It's only useful to know that chained assignment isn't reliable. It's not that it's a reliable, non-optimal solution, the situation is much worse. You've even acknowledged this elsewhere on Stack Overflow. Please try to avoid giving the illusion that chained assignment is a viable option. The first two methods you gave were enough, and are the preferred way to do this. Oct 7, 2013 at 15:55
  • 12
    The internet is serious business. At any rate, EMS, I appreciated knowing the option exists. Oct 9, 2013 at 6:44
  • One issue you might run into is that the csv has periods/dots in the column names and assignments get messed up. You can fix the columns using something like this: cols = df.columns cols = cols.map(lambda x: x.replace('.', '_') if isinstance(x, str) else x) df.columns = cols
    – ski_squaw
    Nov 9, 2016 at 19:29
  • 1
    For those that don't use the .loc syntax much and were a little confused how it worked here, these two short docs helped me get it better: pandas.pydata.org/pandas-docs/stable/reference/api/…, towardsdatascience.com/… Jan 21, 2021 at 4:57
53

You can use map, it can map vales from a dictonairy or even a custom function.

Suppose this is your df:

    ID First_Name Last_Name
0  103          a         b
1  104          c         d

Create the dicts:

fnames = {103: "Matt", 104: "Mr"}
lnames = {103: "Jones", 104: "X"}

And map:

df['First_Name'] = df['ID'].map(fnames)
df['Last_Name'] = df['ID'].map(lnames)

The result will be:

    ID First_Name Last_Name
0  103       Matt     Jones
1  104         Mr         X

Or use a custom function:

names = {103: ("Matt", "Jones"), 104: ("Mr", "X")}
df['First_Name'] = df['ID'].map(lambda x: names[x][0])
5
  • 3
    Won't this generate a KeyError if the values do not exist in your dict?
    – EdChum
    Oct 7, 2013 at 14:04
  • 1
    The custom function will, the others will work anyway. But i assumed the dict is created for the mapping. Otherwise some checking/cleaning can be done based on something like: df.ID.isin(names.keys()) Oct 7, 2013 at 14:12
  • The custom function can be expanded into any (non anonymous) function.
    – user989762
    Feb 12, 2020 at 10:08
  • When I try this on my problem, I get AttributeError: 'DataFrame' object has no attribute 'map'
    – Liz
    Feb 7, 2022 at 15:36
  • 1
    @Liz, that probably means you didn't select a single column (which makes it a Series). To do it on a DataFrame, you might consider using .apply combined with a .map on the resulting series. Feb 8, 2022 at 8:39
34

The original question addresses a specific narrow use case. For those who need more generic answers here are some examples:

Creating a new column using data from other columns

Given the dataframe below:

import pandas as pd
import numpy as np

df = pd.DataFrame([['dog', 'hound', 5],
                   ['cat', 'ragdoll', 1]],
                  columns=['animal', 'type', 'age'])

In[1]:
Out[1]:
  animal     type  age
----------------------
0    dog    hound    5
1    cat  ragdoll    1

Below we are adding a new description column as a concatenation of other columns by using the + operation which is overridden for series. Fancy string formatting, f-strings etc won't work here since the + applies to scalars and not 'primitive' values:

df['description'] = 'A ' + df.age.astype(str) + ' years old ' \
                    + df.type + ' ' + df.animal

In [2]: df
Out[2]:
  animal     type  age                description
-------------------------------------------------
0    dog    hound    5    A 5 years old hound dog
1    cat  ragdoll    1  A 1 years old ragdoll cat

We get 1 years for the cat (instead of 1 year) which we will be fixing below using conditionals.

Modifying an existing column with conditionals

Here we are replacing the original animal column with values from other columns, and using np.where to set a conditional substring based on the value of age:

# append 's' to 'age' if it's greater than 1
df.animal = df.animal + ", " + df.type + ", " + \
    df.age.astype(str) + " year" + np.where(df.age > 1, 's', '')

In [3]: df
Out[3]:
                 animal     type  age
-------------------------------------
0   dog, hound, 5 years    hound    5
1  cat, ragdoll, 1 year  ragdoll    1

Modifying multiple columns with conditionals

A more flexible approach is to call .apply() on an entire dataframe rather than on a single column:

def transform_row(r):
    r.animal = 'wild ' + r.type
    r.type = r.animal + ' creature'
    r.age = "{} year{}".format(r.age, r.age > 1 and 's' or '')
    return r

df.apply(transform_row, axis=1)

In[4]:
Out[4]:
         animal            type      age
----------------------------------------
0    wild hound    dog creature  5 years
1  wild ragdoll    cat creature   1 year

In the code above the transform_row(r) function takes a Series object representing a given row (indicated by axis=1, the default value of axis=0 will provide a Series object for each column). This simplifies processing since you can access the actual 'primitive' values in the row using the column names and have visibility of other cells in the given row/column.

4
  • 1
    Thanks for taking the time to write up such a comprehensive answer. Much appreciated. Jun 22, 2018 at 1:42
  • Thanks for this extremely helpful answer. One follow-up - what if we want to modify a column by doing math on the column, rather than modifying a string? For instance, using the example above, what if we want to multiply the df.age column by 7 if df.animal=='dog'? Thank you!
    – BGG16
    Jul 27, 2020 at 16:51
  • 1
    @GbG: np.where is probably what you are looking for, see e.g. stackoverflow.com/a/42540310/191246 but it's also possible that you won't be able to fit the logic into a scalar operation, then you'd need to explicitly transform the cell numerically similar to how it's done in transform_row
    – ccpizza
    Jul 27, 2020 at 16:55
  • Thank you @ccpizza! Just what I was looking for.
    – BGG16
    Jul 28, 2020 at 15:43
15

This question might still be visited often enough that it's worth offering an addendum to Mr Kassies' answer. The dict built-in class can be sub-classed so that a default is returned for 'missing' keys. This mechanism works well for pandas. But see below.

In this way it's possible to avoid key errors.

>>> import pandas as pd
>>> data = { 'ID': [ 101, 201, 301, 401 ] }
>>> df = pd.DataFrame(data)
>>> class SurnameMap(dict):
...     def __missing__(self, key):
...         return ''
...     
>>> surnamemap = SurnameMap()
>>> surnamemap[101] = 'Mohanty'
>>> surnamemap[301] = 'Drake'
>>> df['Surname'] = df['ID'].apply(lambda x: surnamemap[x])
>>> df
    ID  Surname
0  101  Mohanty
1  201         
2  301    Drake
3  401         

The same thing can be done more simply in the following way. The use of the 'default' argument for the get method of a dict object makes it unnecessary to subclass a dict.

>>> import pandas as pd
>>> data = { 'ID': [ 101, 201, 301, 401 ] }
>>> df = pd.DataFrame(data)
>>> surnamemap = {}
>>> surnamemap[101] = 'Mohanty'
>>> surnamemap[301] = 'Drake'
>>> df['Surname'] = df['ID'].apply(lambda x: surnamemap.get(x, ''))
>>> df
    ID  Surname
0  101  Mohanty
1  201         
2  301    Drake
3  401         
4
  • 1
    this is by far the best and easiest answer I've seen, with excellent default handling. Thank you.
    – Brendan
    Mar 12, 2018 at 22:58
  • @Brendan: Oh! Thanks very much.
    – Bill Bell
    Mar 13, 2018 at 2:59
  • For the people going with this solution, be wary of some pitfalls and possible pains you're exposing yourself to when sub-classing from dict directly.
    – deepbrook
    Nov 18, 2020 at 11:15
  • Probably collections.defaultdict could be used instead of subclassing the builtin dict class. Something like d = {...}; dd = collections.defaultdict(lambda k: '...', d and you can use dd. Nov 21, 2022 at 17:06
9
df['FirstName']=df['ID'].apply(lambda x: 'Matt' if x==103 else '')
df['LastName']=df['ID'].apply(lambda x: 'Jones' if x==103 else '')
2
  • 2
    The community encourages adding explanations to questions and not posting purely code answers (see here).
    – costaparas
    Jan 23, 2021 at 3:45
  • 1
    Yet, in my opinion, this is the best, clearest, cleanest and most concise answer. Mar 22, 2021 at 5:42
1

In case someone is looking for a way to change the values of multiple rows based on some logical condition of each row itself, using .apply() with a function is the way to go.

df = pd.DataFrame({'col_a':[0,0], 'col_b':[1,2]})

   col_a  col_b
0      0      1
1      0      2

def func(row):
    if row.col_a == 0 and row.col_b <= 1:
        row.col_a = -1
        row.col_b = -1
    return row

df.apply(func, axis=1)

   col_a  col_b
0     -1     -1 # Modified row
1      0      2

Although .apply() is typically used to add a new row/column to a dataframe, it can be used to modify the values of existing rows/columns.

0

I found it much easier to debut by printing out where each row meets the condition:

for n in df.columns:
    if(np.where(df[n] == 103)):
        print(n)
        print(df[df[n] == 103].index)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.