仅在datafram中计算少数列的LOG10

2024-09-29 19:30:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把给定的观察值转换成log10。你知道吗

这是我的国防部长: enter image description here

我只想计算HR90和左边的字段的log10。我试图只选择正确的列,但我得到错误的所有时间,我瘦我的代码太复杂了,这一点。你知道吗

这是我的密码:

for i[9:] in data.columns:
 np.log10(data)

AttributeError:“str”对象没有属性“log10”

似乎它还抓取了我不想在其上运行此进程的列。 我还尝试导入数学,然后:

import math
for i in data.columns:
    if(data[i].dtype == np.float64 or data[i].dtype == np.int64):
        data.applymap(math.log10)

但是我得到了:

TypeError: ('must be real number, not str', 'occurred at index NAME')

我的最终目标是将我的部分观察结果转换成LOG10


Tags: columns代码in密码fordata错误np
3条回答

你是说这样的事吗?你知道吗

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

这也会给你同样的:

df[df.columns[:3]] = df[df.columns[:3]].apply(np.log10)

简单点怎么样

data[data.columns[:9]] = np.log10(data[data.columns[:9]])

对于索引为0到索引为8(含)的所有列,这将使用log10。你知道吗

如果列左移或等于“HR90”,则可以使用get_loc应用对数映射。你知道吗

for i in data.columns:
    if data.columns.get_loc(i) <= data.columns.get_loc('HR90'):
        data[i] = data[i].map(math.log10)

相关问题 更多 >

    热门问题