1

pandas.read_excel()我在 Python 中使用导入了一个 Excel 文件。
然后我想对特定列中的每个数字进行数学计算,并生成一个新列。但是有一个错误:

TypeError:无法将系列转换为

我该如何解决这个问题?下面是我的代码。

import pandas as pd
import math

N_DATA=pd.read_excel(r"path\datajl.xls",index_col='R')
rchdecay=N_DATA['column_name']
rchdcayf=math.exp(-rchdecay*0.008)
4

1 回答 1

1

我认为你需要numpy.exp

import numpy as np

rchdecay=N_DATA['column_name']
rchdcayf=np.exp(-rchdecay*0.008)

样本:

import pandas as pd
import numpy as np

N_DATA = pd.DataFrame({'column_name':[1,2,3]})
print (N_DATA)
   column_name
0            1
1            2
2            3

rchdcayf=np.exp(-N_DATA['column_name']*0.008)
print (rchdcayf)
0    0.992032
1    0.984127
2    0.976286
Name: column_name, dtype: float64

或,但速度较慢:apply math.exp

rchdcayf1=(-N_DATA['column_name']*0.008).apply(math.exp)
print (rchdcayf1)
0    0.992032
1    0.984127
2    0.976286
Name: column_name, dtype: float64

时间

len(df)=3

In [61]: %timeit (-N_DATA['column_name']*0.008).apply(math.exp)
The slowest run took 5.46 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 209 µs per loop

In [62]: %timeit np.exp(-N_DATA['column_name']*0.008)
The slowest run took 4.59 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 168 µs per loop

len(df)=3k

In [64]: %timeit np.exp(-N_DATA['column_name']*0.008)
1000 loops, best of 3: 214 µs per loop

In [65]: %timeit (-N_DATA['column_name']*0.008).apply(math.exp)
1000 loops, best of 3: 873 µs per loop

计时码

import pandas as pd
import numpy as np
import math

N_DATA = pd.DataFrame({'column_name':[1,2,3]})
N_DATA = pd.concat([N_DATA]*1000).reset_index(drop=True)

rchdcayf=np.exp(-N_DATA['column_name']*0.008)
print (rchdcayf)

rchdcayf1=(-N_DATA['column_name']*0.008).apply(math.exp)
print (rchdcayf1)
于 2016-06-02T14:10:51.470 回答