How to modify cells in a pandas DataFrame?

10,344

Solution 1

If need modify all columns in DataFrame use numpy.where with DataFrame constructor, because where return numpy array:

df = pd.DataFrame(np.where(df == 'something', df + 'add a string', df + 'add a value'), 
                  index=df.index, 
                  columns=df.columns)

If only one column col:

df['col'] = np.where(df['col'] == 'something', 
                     df['col'] + 'add a string', 
                     df['col'] + 'add a value')

Sample:

df = pd.DataFrame({'col': ['a', 'b', 'a'], 'col1': ['a', 'b', 'b']})
print (df)
  col col1
0   a    a
1   b    b
2   a    b

df = pd.DataFrame(np.where(df == 'a', df + 'add a string', df + 'add a value'), 
                  index=df.index, 
                  columns=df.columns)
print (df)
             col           col1
0  aadd a string  aadd a string
1   badd a value   badd a value
2  aadd a string   badd a value

df['col'] = np.where(df['col'] == 'a', 
                     df['col'] + 'add a string', 
                     df['col'] + 'add a value')

print (df)
             col col1
0  aadd a string    a
1   badd a value    b
2  aadd a string    b

Solution 2

You can use .ix and apply a function like this:

import pandas as pd

D = pd.DataFrame({'A': ['a', 'b', 3,7,'b','a'], 'B': ['a', 'b', 3,7,'b','a']})

D.ix[D.index%2 == 0,'A'] = D.ix[D.index%2 == 0,'A'].apply(lambda s: s+'x' if isinstance(s,str) else s+1)

D.ix[D.index[2:5],'B'] = D.ix[D.index[2:5],'B'].apply(lambda s: s+'y' if isinstance(s,str) else s-1)

First example appends x to each string or alternatively adds 1 to each non-string on column A for every even index.

The second example appends y to each string or alternatively subtracts 1 from each non-string on column B for the indices 2,3,4.

Original Frame:

   A  B
0  a  a
1  b  b
2  3  3
3  7  7
4  b  b
5  a  a

Modified Frame:

    A   B
0  ax   a
1   b   b
2   4   2
3   7   6
4  bx  by
5   a   a
Share:
10,344
Kubik Straka
Author by

Kubik Straka

Updated on July 24, 2022

Comments

  • Kubik Straka
    Kubik Straka almost 2 years

    I need to change individual elements in a DataFrame. I tried doing something like this, but it doesn't work:

    for index, row in df.iterrows():
        if df.at[row, index] == 'something':
            df.at[row, index] = df.at[row, index] + 'add a string'
        else:
            df.at[row, index] = df.at[row, index] + 'add a value'
    

    How can I do that?