如何将操作数据从 .fits 文件转换为 pandas DataFrame
How to convert manipulated data from .fits file to pandas DataFrame
我有一个包含一些数据的 .fits 文件,我从中进行了一些操作并想将新数据(不是整个 .fits 文件)存储为 pd.DataFrame。数据来自名为 pabdatazcut.fits.
的文件
#Sorted by descending Paschen Beta flux
sortedpab = sorted(pabdatazcut[1].data , key = lambda data: data['PAB_FLUX'] , reverse = True )
unsorteddf = pd.DataFrame(pabdatazcut[1].data)
sortedpabdf = pd.DataFrame({'FIELD' : sortedpab['FIELD'],
'ID' : sortedpab['ID'],
'Z_50' : sortedpab['Z_50'],
'Z_ERR' : ((sortedpab['Z_84'] - sortedpab['Z_50']) + (sortedpab['Z_50'] - sortedpab['Z_16'])) / (2 * sortedpab['Z_50']),
'$\lambda Pa\beta$' : 12820 * (1 + sortedpab['Z_50']),
'$Pa\beta$ FLUX' : sortedpab['PAB_FLUX'],
'$Pa\beta$ FLUX ERR' : sortedpab['PAB_FLUX_ERR']})
''''
我在尝试 运行 时收到 'TypeError: list indices must be integers or slices, not str' 错误消息。
你得到这个是因为我猜 sortedpab['ID']
这样的访问。根据文档 sorted
returns 排序列表。列表不接受字符串作为 id
来访问元素。它们只能通过整数位置或切片访问。这就是错误试图告诉您的内容。
不幸的是我不能在我的机器上测试这个,因为我没有你的数据,但我想,你真正想要做的是这样的:
data_dict= dict()
for obj in sortedpab:
for key in ['FIELD', 'ID', 'Z_50', 'Z_50', 'Z_ERR', 'Z_84', 'PAB_FLUX', 'PAB_FLUX_ERR']:
data_dict.setdefault(key, list()).append(obj[key])
sortedpabdf = pd.DataFrame(data_dict)
# maybe you don't even need to create the data_dict but
# can pass the sortedpad directly to your data frame
# have you tried that already?
#
# then I would calculate the columns which are not just copied
# in the dataframe directly, as this is more convenient
# like this:
sortedpabdf['Z_ERR']= ((sortedpabdf['Z_84'] - sortedpabdf['Z_50']) + (sortedpabdf['Z_50'] - sortedpabdf['Z_16'])) / (2 * sortedpabdf['Z_50'])
sortedpabdf['$\lambda Pa\beta$']= 12820 * (1 + sortedpabdf['Z_50']),
sortedpabdf.rename({
'PAB_FLUX': '$Pa\beta$ FLUX',
'PAB_FLUX_ERR': '$Pa\beta$ FLUX ERR'
}, axis='columns', inplace=True)
cols_to_delete= [col for col in sortedpabdf.columns if col not in ['FIELD', 'ID', 'Z_50', 'Z_ERR', '$\lambda Pa\beta$', '$Pa\beta$ FLUX','$Pa\beta$ FLUX ERR'])
sortedpabdf.drop(cols_to_delete, axis='columns', inplace=True)
我有一个包含一些数据的 .fits 文件,我从中进行了一些操作并想将新数据(不是整个 .fits 文件)存储为 pd.DataFrame。数据来自名为 pabdatazcut.fits.
的文件#Sorted by descending Paschen Beta flux
sortedpab = sorted(pabdatazcut[1].data , key = lambda data: data['PAB_FLUX'] , reverse = True )
unsorteddf = pd.DataFrame(pabdatazcut[1].data)
sortedpabdf = pd.DataFrame({'FIELD' : sortedpab['FIELD'],
'ID' : sortedpab['ID'],
'Z_50' : sortedpab['Z_50'],
'Z_ERR' : ((sortedpab['Z_84'] - sortedpab['Z_50']) + (sortedpab['Z_50'] - sortedpab['Z_16'])) / (2 * sortedpab['Z_50']),
'$\lambda Pa\beta$' : 12820 * (1 + sortedpab['Z_50']),
'$Pa\beta$ FLUX' : sortedpab['PAB_FLUX'],
'$Pa\beta$ FLUX ERR' : sortedpab['PAB_FLUX_ERR']})
''''
我在尝试 运行 时收到 'TypeError: list indices must be integers or slices, not str' 错误消息。
你得到这个是因为我猜 sortedpab['ID']
这样的访问。根据文档 sorted
returns 排序列表。列表不接受字符串作为 id
来访问元素。它们只能通过整数位置或切片访问。这就是错误试图告诉您的内容。
不幸的是我不能在我的机器上测试这个,因为我没有你的数据,但我想,你真正想要做的是这样的:
data_dict= dict()
for obj in sortedpab:
for key in ['FIELD', 'ID', 'Z_50', 'Z_50', 'Z_ERR', 'Z_84', 'PAB_FLUX', 'PAB_FLUX_ERR']:
data_dict.setdefault(key, list()).append(obj[key])
sortedpabdf = pd.DataFrame(data_dict)
# maybe you don't even need to create the data_dict but
# can pass the sortedpad directly to your data frame
# have you tried that already?
#
# then I would calculate the columns which are not just copied
# in the dataframe directly, as this is more convenient
# like this:
sortedpabdf['Z_ERR']= ((sortedpabdf['Z_84'] - sortedpabdf['Z_50']) + (sortedpabdf['Z_50'] - sortedpabdf['Z_16'])) / (2 * sortedpabdf['Z_50'])
sortedpabdf['$\lambda Pa\beta$']= 12820 * (1 + sortedpabdf['Z_50']),
sortedpabdf.rename({
'PAB_FLUX': '$Pa\beta$ FLUX',
'PAB_FLUX_ERR': '$Pa\beta$ FLUX ERR'
}, axis='columns', inplace=True)
cols_to_delete= [col for col in sortedpabdf.columns if col not in ['FIELD', 'ID', 'Z_50', 'Z_ERR', '$\lambda Pa\beta$', '$Pa\beta$ FLUX','$Pa\beta$ FLUX ERR'])
sortedpabdf.drop(cols_to_delete, axis='columns', inplace=True)