簡體   English   中英

將 pandas object 轉換為浮點數

[英]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]

所以,在這個階段,我有一個數組([['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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM