将对象转换为浮点

2024-05-13 08:57:21 发布

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

我试图编写一个代码,可以读取excel文件,找到与我想要的数据对应的行,然后保存该行的最后一个值

我使用的代码是:

import pandas as ap
import numpy as np

#Read the Excel file
excel = ap.read_excel(r'EXAMPLE')

#Columns and rows selections
tec_data = excel.iloc[0::, 0:6]

#creating a numpy array with the table
np_array = tec_data.to_numpy()

#Found the line that corresponds to the pretended simulation data
found_tec_line = np.argwhere((month==np_array[:,0]) & (hour==np_array[:,1]) & (azimuth==np_array[:,2]) & (elevation==np_array[:,3]) & (height==np_array[:,4]))

#Save TEC
tec = np_array[found_tec_line,5]

因此,在这个阶段,我有一个tec,它是一个数组([['27.8*10**15']],dtype=object) 我只想把27.89*10**15保存为浮动,但我不知道怎么做。 我使用了tec=tec.astype(float),但它说“无法将字符串转换为float:'27.89*10**15'”

注意:代码中的数字和*之间没有空格,我在这里引入空格只是因为自动加粗


Tags: theto代码importnumpydataasnp
1条回答
网友
1楼 · 发布于 2024-05-13 08:57:21
import pandas as pd

tec = np_array[found_tec_line,5]
tec=pd.eval(tec)

通过Dataframe()方法和apply()方法尝试:

tec = np_array[found_tec_line,5]

tec=pd.Dataframe(tec).apply(pd.eval).values

相关问题 更多 >