[英]How can I import certain files from a folder by creating a mask?
I'm incredibly new to coding so please bear with me so basically I have a folder of 4,229 .fits files (legac_spec) and a dataframe (legac_cat) with 1989 rows, two of the columns being id number and mask value.我对编码非常陌生,所以请耐心等待,所以基本上我有一个包含 4,229 个 .fits 文件(legac_spec)的文件夹和一个包含 1989 行的数据框(legac_cat),其中两列是 ID 号和掩码值。 Each .fits file has a file name of something along the lines of
legac_M[mask value]_v3.6_spec1d _[id number].fits
but I'm not sure how to get the specific files I need where each mask value and id number corresponds to a specific file.每个 .fits 文件都有一个类似于
legac_M[mask value]_v3.6_spec1d _[id number].fits
的文件名,但我不确定如何获取我需要的特定文件,其中每个掩码值和 ID 号对应一个特定的文件。
I know I need to use a for loop but I'm not sure how I get it to do what i need it to do since I have to mask the mask value and id numbers separately我知道我需要使用 for 循环,但我不确定如何让它做我需要做的事情,因为我必须分别屏蔽掩码值和 ID 号
import numpy as np
import pandas as pd
from astropy.io import fits
legac_cat = pd.read_csv('legac_file')
M = legac_cat['mask']
ID = legac_cat.id
directory_name = 'C:/Users/kfdhfs/Downloads/legac_spec'
for mask_val in legac_cat['mask']:
for files in directory_name:
hdu = fits.open(files)
I've barely ever used Pandas.我几乎没用过 Pandas。 I haven't tested this solution, so sorry if it doesn't work "straight out of the box".
我还没有测试过这个解决方案,如果它不能“直接开箱即用”,那么很抱歉。 It seems to me that all you have to do is iterate over your dataframe's rows (where each row has a "mask" and "id" that correspond to one file), and then construct a filename from each row's "mask" and "id" - then open the file:
在我看来,您所要做的就是遍历数据帧的行(其中每一行都有一个对应于一个文件的“掩码”和“id”),然后从每一行的“掩码”和“id”构造一个文件名" - 然后打开文件:
legac_cat = pd.read_csv("legac_file")
for index, row in legac_cat.iterrows():
file_name = "legac_M{}_v3.6_spec1d_{}.fits".format(row["mask"], row["id"])
# open the file using file_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.