Pythonpandasdf.min()返回inf

我一直在研究Python代码从CSV文件中获取数据并将其粘贴到新的Excel文档中。 我正在使用Python 2.7,Numpy和Pandas。

我能够:

  • 读取CSV文件
  • 隔离我感兴趣的列
  • 写入一个新的Excel文件

我无法:

  • find隔离栏内的最小值

我试过的:

  • 谷歌
  • 尝试将infreplace为NaN并删除

在以下代码后添加了以下几行:

print(DTFRL.min()) 

代码行

 DTFRL.replace([np.inf, -np.inf], np.nan) DTFRL.replace([np.inf, -np.inf], np.nan).dropna(subset=["col1", "col2"], how="all") 

我的代码没有上述添加如下:

 # James Hayek # Takes input from a CSV file and exports the data to # an Excel document # Goals: # 1. Import the highest or lowest value in CSV file # 2. Place the value in a specific column in an Excel file # 3. Incriment through a slew of CSV files # 4. Open all CSV files from a file path that the user picks import numpy as np import pandas as pd pd.set_option('display.mpl_style', 'default') # Make the graphs a bit prettier. See warning, change later #This line reads the entire data set from the CSV file DTFRL_df = pd.read_csv('C:\\Users\\James\\OneDrive\\Documents\\cSquared\\ExcelPythonAutomation\\CSVFiles\\31-SPT76-ANT123-DTF.csv', sep=',', error_bad_lines=False) DTFRL = DTFRL_df.iloc[0:1100,1] #Setting a variable for the DTF-RL column #print(DTFRL_df.iloc[0:1100,1]) #This prints all the rows and just the second column print(DTFRL.min()) #Atempting to print the lowest value in the second column, but getting inf instead DTFRL.replace([np.inf, -np.inf], np.nan) DTFRL.replace([np.inf, -np.inf], np.nan).dropna(subset=["col1", "col2"], how="all") #This line writes the variable DTRFL_df to a new Excel file, can change variable to write later #to make sure we just write the lowest value found DTFRL.min().to_excel('C:\\Users\\James\\OneDrive\\Documents\\cSquared\\ExcelPythonAutomation\\test.xlsx') 

我的问题:

  • 我需要在.dropna(subset = [“?”,“?”])中指定根据返回的inf值返回到最低的浮点数?

先谢谢您的帮助

这里是重现问题的CSV文件的头部。

标题,副标题,
 DTF-RL ,,
参数名称,设置,
分辨率:,1033,
 CAL:,开(OSL-Std),
 RF免疫:高,
标准:,无,
频率:开始/停止:,698.0MHz / 2700.0MHz,
date:,“2015年12月17日星期四”,
时间:,8:08:26 PM,
macros。 损耗:,0.045 dB / ft,
型号:,S331L,
串行:,1307024,
 Prop.Vel:,0.865,
标记数据
标记,距离(英尺),价值
 M1,85.63468992,-1.017566517
限制,价值:25.00,
点数据
距离(英尺),DTF-RL,
 0,43.37040226,
 0.121124031,41.60694628,
 0.242248062,42.73552301,
 0.363372093,49.25223571,
 0.484496124,65.76120197,
 0.605620155,71.70100065,
 0.726744186,68.81150462,

该文件可以在这里访问。

您可以使用isnumeric()过滤非数字号码

 import csv import pandas as pd from io import StringIO data = "out.txt" df = pd.read_csv(data,header=0,encoding ='utf-8') print df df_out= df[df.id.apply(lambda x: x.isnumeric())] print df_out 

out.txt

 id,name 1,A 2,B 3,C tt,D 4,E 5,F de,G 

创build输出

  id name 0 1 A 1 2 B 2 3 C 4 4 E 5 5 F