Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding one to all the values in a dataframe

I have a dataframe like the one below. I would like to add one to all of the values in each row. I am new to this forum and python so i can't conceptualise how to do this. I need to add 1 to each value. I intend to use bayes probability and the posterior probability will be 0 when i multiply them. PS. I am also new to probability but others have applied the same method. Thanks for your help in advance. I am using pandas to do this.

Disease  Gene1  Gene2  Gene3 Gene4 
D1    0    0    25   0  
D2    0    0    0    0  
D3    0    17   0    16  
D4    24   0    0    0  
D5    0    0    0    0  
D6    0    32   0    11  
D7    0    0    0    0  
D8    4    0    0    0
like image 628
kheston Walkins Avatar asked Jun 12 '15 02:06

kheston Walkins


2 Answers

With this being your dataframe:

df = pd.DataFrame({
    "Disease":[f"D{i}" for i in range(1,9)],
    "Gene1":[0,0,0,24,0,0,0,4],
    "Gene2":[0,0,17,0,0,32,0,0],
    "Gene3":[25,0,0,0,0,0,0,0],
    "Gene4":[0,0,16,0,0,11,0,0]})

  Disease  Gene1  Gene2  Gene3  Gene4
0      D1      0      0     25      0
1      D2      0      0      0      0
2      D3      0     17      0     16
3      D4     24      0      0      0
4      D5      0      0      0      0
5      D6      0     32      0     11
6      D7      0      0      0      0
7      D8      4      0      0      0

The easiest way to do this is to do

df += 1

However, since you have a column which is string (The Disease column)

This will not work.

But we can conveniently set the Disease column to be the index, like this:

df.set_index('Disease', inplace=True)

Now your dataframe looks like this:

         Gene1  Gene2  Gene3  Gene4
Disease                            
D1           0      0     25      0
D2           0      0      0      0
D3           0     17      0     16
D4          24      0      0      0
D5           0      0      0      0
D6           0     32      0     11
D7           0      0      0      0
D8           4      0      0      0

And if we do df += 1 now, we get:

         Gene1  Gene2  Gene3  Gene4
Disease                            
D1           1      1     26      1
D2           1      1      1      1
D3           1     18      1     17
D4          25      1      1      1
D5           1      1      1      1
D6           1     33      1     12
D7           1      1      1      1
D8           5      1      1      1

because the plus operation only acts on the data columns, not on the index.

You can also do this on column basis, like this:

df["Gene1"] = df["Gene1"] + 1
like image 71
firelynx Avatar answered Nov 18 '22 14:11

firelynx


You can filter the df whether the underlying dtype is not 'object':

In [110]:
numeric_cols = [col for col in df if df[col].dtype.kind != 'O']
numeric_cols

Out[110]:
['Gene1', 'Gene2', 'Gene3', 'Gene4']

In [111]:    
df[numeric_cols] += 1
df

Out[111]:
  Disease  Gene1  Gene2  Gene3  Gene4
0      D1      1      1     26      1
1      D2      1      1      1      1
2      D3      1     18      1     17
3      D4     25      1      1      1
4      D5      1      1      1      1
5      D6      1     33      1     12
6      D7      1      1      1      1
7      D8      5      1      1      1

EDIT

It looks like your df possibly has strings instead of numeric types, you can convert the dtype to numeric using convert_objects:

df = df.convert_objects(convert_numeric=True)
like image 22
EdChum Avatar answered Nov 18 '22 15:11

EdChum