我一直在使用 Python 代码从 CSV 文件中获取数据并将其粘贴到新的 Excel 文档中。我正在使用 Python 2.7、Numpy 和 Pandas。
我能够:
- 阅读CSV文件
- 隔离我感兴趣的列
- 写入一个新的 Excel 文件
我无法:
- 在那个孤立的列中找到最小值
我尝试过的:
- 谷歌
- 尝试将 inf 替换为 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=["?", "?"]) 中指定什么来消除与最低 float 相对的返回的 inf 值?
在此先感谢您的帮助
这是重现问题的 CSV 文件的头部。
Title,SubTitle, DTF-RL,, Parameter Name,Setting, Resolution:,1033, CAL:,On (OSL-Std), RF Immunity:,High, Std:,None, Freq: Start/Stop:,698.0 MHz/2700.0 MHz, Date:,"Thursday, December 17, 2015", Time:,8:08:26 PM, Ins. Loss:,0.045 dB/ft, Model:,S331L, Serial:,1307024, Prop.Vel:,0.865, Marker Data,, Marker,Distance( ft),Value M1,85.63468992,-1.017566517 Limit On,Value: 25.00, Point Data,, Distance( ft),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,
文件可以访问here .
请您参考如下方法:
您可以使用 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
输出.txt
id,name
1,A
2,B
3,C
tt,D
4,E
5,F
de,G
创建输出
id name
0 1 A
1 2 B
2 3 C
4 4 E
5 5 F
