将熊猫对象转换为浮点数

时间:2021-05-23 00:39:50

标签: python python-3.x pandas dataframe numpy-ndarray

我正在尝试编写一个可以读取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'”

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

1 个答案:

答案 0 :(得分:1)

import sys
import collections

counter = collections.Counter()

for line in sys.stdin:
    word, count = line.strip().split("\t", 1)

    counter[word] += int(count)

for x in counter.most_common(9999):
    print(x[0],"\t",x[1])

通过 import pandas as pd tec = np_array[found_tec_line,5] tec=pd.eval(tec) 方法和 Dataframe() 方法尝试:

apply()