我有这样一个数据文件:# 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
所需的输出文件output.csv是:
^{pr2}$
我们怎样才能在Python熊猫或纽比中做到这一点呢?在
我的初步尝试是:#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author : Bhishan Poudel
# Date : June 16, 2016
# Imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#==============================================================================
# read in a file
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)
# find value of wavelength just above 0.05
print("\n")
df = df[(df['trans'] >= 0.05) ]
print(df)