Reputation: 653
I have been working on a Python code to take data from a CSV file and paste it to a new Excel document. I am using Python 2.7, Numpy and Pandas.
I am able to:
I am unable to:
What I tried:
I added the following lines after the:
print(DTFRL.min())
line of code
DTFRL.replace([np.inf, -np.inf], np.nan)
DTFRL.replace([np.inf, -np.inf], np.nan).dropna(subset=["col1", "col2"], how="all")
My code without the above addition is as follows:
# 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')
My Question:
Thanks in advance for the help
Here is the head of the CSV file to reproduce the issue.
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,
The file can be accessed here.
Upvotes: 2
Views: 1523
Reputation: 9711
you can filter non numeric number using 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
creates the output
id name
0 1 A
1 2 B
2 3 C
4 4 E
5 5 F
Upvotes: 2