Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify cells in a pandas DataFrame?

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?

like image 733
Kubik Straka Avatar asked Aug 19 '16 08:08

Kubik Straka


People also ask

How do I change a cell value in pandas?

You can set cell value of pandas dataframe using df.at[row_label, column_label] = 'Cell Value'. It is the fastest method to set the value of the cell of the pandas dataframe.

Can we modify data inside a data frame?

Using Python replace() method, we can update or change the value of any string within a data frame. We need not provide the index or label values to it. As seen above, we have replaced the word “Siri” with “Code” within the dataframe.


2 Answers

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
like image 105
jezrael Avatar answered Oct 14 '22 03:10

jezrael


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
like image 37
Khris Avatar answered Oct 14 '22 03:10

Khris