Cam
Cam

Reputation: 121

Perform log2 normalization over columns in dataframe

Is there a way to use the np.log2() function to iterate over columns of a dataframe while keeping column names?

df = pd.DataFrame(np.random.randint(0,100,size=(100, 10)), columns=list('ABCDEFGHIJ'))
print(df)

     A   B   C   D   E   F   G   H   I   J
0   62  77  38  89  20  38  20  86  13  72
1   40  11   8  39   5  15  30  90  54  87
2   43  28  93  96   1  76  48  98  98  64
3   67  25  55  31  76  43  86  45  81  43
4   85  72  10  38  27  55   7  35   6   2
..  ..  ..  ..  ..  ..  ..  ..  ..  ..  ..
95  71  27  22  21  65  64  66  21  30  11
96   3  81  69  37  40  74  49   0   2  73
97  43  49  47  73  44  24  47  99  72   9
98  62  89   3  85  50  63  80  18  39  13
99  73  37  45  81  52  72  10   7  28  66

[100 rows x 10 columns]

I want to normalize these columns to perform functions on them later for data analysis without doing this by hand like:

a = df['A'] 
alog = np.log2(a + 0.001)

Could I do this normalization without writing it by hand ten times?

Upvotes: 1

Views: 523

Answers (1)

Amirhossein
Amirhossein

Reputation: 92

for i in list('ABCDEFGHIJ'):
    df[i] = np.log2(df[i]+0.001) 

df:

enter image description here

Upvotes: 1

Related Questions