I have a data file similar to this:
# column 1 is the angle of incidence (degrees)
# column 2 is the wavelength (microns)
# column 3 is the transmission probability
# column 4 is the reflection probability
14.2000 0.300000 0.01 0.999920
14.2000 0.301000 0.02 0.999960
14.2000 0.302000 0.03 0.999980
14.2000 0.303000 0.04 0.999980
14.2000 0.304000 0.06 0.999980
14.2000 0.305000 0.08 0.999970
14.2000 0.306000 0.2 0.999950
14.2000 0.307000 0.4 0.999910
14.2000 0.308000 0.8 0.999860
14.2000 0.309000 0.9 0.999960
14.2000 0.310000 0.8 0.999990
14.2000 0.311000 0.4 0.999980
14.2000 0.312000 0.2 0.999960
14.2000 0.313000 0.06 0.999940
14.2000 0.314000 0.03 0.999930
14.2000 0.315000 0.02 1.00000
14.2000 0.316000 0.01 1.00000
Required output file output.csv:
14.2000 0.304000 0.06 0.999980
14.2000 0.305000 0.08 0.999970
14.2000 0.306000 0.2 0.999950
14.2000 0.307000 0.4 0.999910
14.2000 0.308000 0.8 0.999860
14.2000 0.309000 0.9 0.999960
14.2000 0.310000 0.8 0.999990
14.2000 0.311000 0.4 0.999980
14.2000 0.312000 0.2 0.999960
14.2000 0.313000 0.06 0.999940
14.2000 0.314000 0.03 0.999930
How to do it in python pandas or numpy?
My initial attempt:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
infile = 'filter_2.txt'
colnames = ['angle', 'wave','trans', 'refl']
print('{} {} {} {}'.format('\nreading file : ', infile, '','' ))
df = pd.read_csv(infile,sep='\s+', header = None,skiprows = 0,
comment='#',names=colnames,usecols=(0,1,2,3))
print(df)
print("\n")
df = df[(df['trans'] >= 0.05) ]
print(df)
Some related links are as follows:
How to read between two specific lines in python
source
share