将 pandas 对象转换为浮点数
Convert a pandas object into float
我试图制作一个可以读取 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),但它说“无法将字符串转换为浮点数:'27.89 * 10* * 15'”
注意:代码中的数字和*之间没有空格,我在这里引入空格只是因为自动加粗
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
我试图制作一个可以读取 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),但它说“无法将字符串转换为浮点数:'27.89 * 10* * 15'”
注意:代码中的数字和*之间没有空格,我在这里引入空格只是因为自动加粗
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