Reputation: 1592
I want to convert my given observations to log10.
I want to calculate the log10 only for the fields from HR90 and left. I have tried to choose only the correct columns but I get error all the time and I thin my code is too much complex for this.
This is my code:
for i[9:] in data.columns:
np.log10(data)
AttributeError: 'str' object has no attribute 'log10'
It seems like it still grabs also columns that I don't want to run this process on them. I have also tried to import math and then:
import math
for i in data.columns:
if(data[i].dtype == np.float64 or data[i].dtype == np.int64):
data.applymap(math.log10)
but thehn I have gotten:
TypeError: ('must be real number, not str', 'occurred at index NAME')
My end goal is to convert part of my observations to LOG10
Upvotes: 3
Views: 1801
Reputation: 58
Do you mean something like this?
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
>>>>
A B C D
0 19 44 16 46
1 25 35 35 51
2 11 67 3 27
3 42 63 81 64
4 91 70 2 77
df[['A', 'B', 'C']] = df[['A', 'B', 'C']].apply(np.log10)
>>>
A B C D
0 0.106787 0.215757 0.080670 46
1 0.145489 0.188666 0.188666 51
2 0.017615 0.261519 -0.321371 27
3 0.210385 0.255113 0.280689 64
4 0.292044 0.266019 -0.521390 77
5 0.263046 0.223262 0.204679 63
This also will give you the same:
df[df.columns[:3]] = df[df.columns[:3]].apply(np.log10)
Upvotes: 2
Reputation: 14546
You can use get_loc to apply the logarithm map if the column is left or equal to 'HR90'.
for i in data.columns:
if data.columns.get_loc(i) <= data.columns.get_loc('HR90'):
data[i] = data[i].map(math.log10)
Upvotes: 0
Reputation: 3093
How about simply
data[data.columns[:9]] = np.log10(data[data.columns[:9]])
This will take log10 for all columns with index 0 to index 8 inclusive.
Upvotes: 1