gpt4 book ai didi

python - 如何使用 numpy 或 pandas 仅在 python 中的两行之间读取数据?

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:29 25 4
gpt4 key购买 nike

我有一个这样的数据文件:

# 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是这样的:

# 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.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


# conditions are:
# output first element of column3 >= 0.05 i.e. 0.06
# output last element of column3 < 0.05 i.e. 0.03

# for the second may be we need to get the index of second 0.06 and
# get the value of next index.

我们如何在 python pandas 或 numpy 中这样做?

我最初的尝试是这样的:

#!/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)

一些类似的链接如下:
How to read between 2 specific lines in python

最佳答案

我会完全跳过 pandas 或 numpy

fo = open('filter_3.txt', 'w')
with open('filter_2.txt', 'r') as fi:
line = fi.readline()
while line:
split = line.split()
if (split[0] == '#') or (float(split[2]) >= 0.027):
print line,
fo.write(line)

line = fi.readline()

fo.close()

# 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.302000 0.028 0.999980
14.2000 0.303000 0.030 0.999980
14.2000 0.304000 0.032 0.999980
14.2000 0.305000 0.030 0.999970
14.2000 0.306000 0.028 0.999950

新增一行代码

fo = open('filter_3.txt', 'w')
with open('filter_2.txt', 'r') as fi:
new_line = fi.readline()
old_line = None
while new_line:
split_new = new_line.split()
if old_line is not None:
split_old = old_line.split()

cond0 = False if old_line is None else (split_old[0] == '#')
cond1 = split_new[0] == '#'
cond2 = float(split_new[2]) >= 0.05
cond3 = False if old_line is None else (float(split_old[2]) >= 0.05)

if (cond1 or cond2) or (cond3 and not cond0):
print new_line,
fo.write(new_line)
printed_old = True

old_line = new_line
new_line = fi.readline()

fo.close()

# 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.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

关于python - 如何使用 numpy 或 pandas 仅在 python 中的两行之间读取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37867657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com