如何将 astropy.table.table.Table 文件类型转换为 pandas.core.frame.DataFrame 文件类型

how to convert astropy.table.table.Table file type to pandas.core.frame.DataFrame file type

我有 10 个文件,所有文件都是 astropy.table.table.Table 文件类型,全部由相同的六列(mjd、filter、flux、flux_error、zp、zpsys)组成,但长度不同。 首先,我想将每个文件转换为 pandas.core.frame.DataFrame 文件类型,以便我可以将它们全部添加到一个列表中,并使用 pd.concat 函数将所有 10 个文件转换为 1 个大 pandas.core.frame.DataFrame 文件。 我试过这个:

import numpy as np
import pandas as pd
from astropy.table import Table

n=10
li=[]
for i in range(0,n):
    file = "training_data/%s.dat"%i # This way I can call each file automatically
    data = Table.read(file, format="ascii") 
    data = pd.read_table(file) # I convert the file to pandas compatible
    li.append(data) # I add the file into the empty list above
    # now I have my list ready so I compress it into 1 file
all_data = pd.concat(li)

此方法的问题是由于某种原因所有列(6 列)都被压缩为 1 列,这使我无法完成其余的工作。

当我检查 all_data 的形状时,我得到 (879, 1)。 它看起来像这样:

all_data.head()

    mjd filter flux flux_error zp zpsys
0   0.0 desg -4.386 4.679 27.5 ab
1   0.011000000005878974 desr -0.5441 2.751 27.5 ab
2   0.027000000001862645 desi 0.4547 4.627 27.5 ab
3   0.043000000005122274 desz -1.047 4.462 27.5 ab
4   13.043000000005122 desg -4.239 4.366 27.5 ab

那么我如何制作这样的文件,同时将我的列保持为单独的列?

这是我在文件 0 中的一些数据的示例:

    mjd     filter  flux   flux_error zp    zpsys
    float64     str4    float64 float64 float64 str2
    0.0       desg      -4.386  4.679   27.5    ab
    0.0110000 desr  -0.5441 2.751   27.5    ab
    0.0270000 desi  0.4547  4.627   27.5    ab
    0.0430000 desz  -1.047  4.462   27.5    ab
    13.043000 desg  -4.239  4.366   27.5    ab
    13.050000 desr  4.695   3.46    27.5    ab
    13.058000 desi  6.291   6.248   27.5    ab
    13.074000 desz  6.412   5.953   27.5    ab
    21.050000 desg  1.588   2.681   27.5    ab
    21.058000 desr  -0.6124 2.171   27.5    ab

可能是 Table.read() 无法猜测您的数据的格式/分隔符。我能够使用 Table.read(file, format='ascii', data_start=2) 将包含的示例(文件 0 中的数据)读取到具有 6 列的 table 中,但我不确定是否正确捕获了空格。

我怀疑文件 0 中的示例数据实际上不是您正在阅读的内容,因为如果没有 data_start=2,该文件将显示第 1 行 "float64 str4 float64 float64 float64 str2".

你可以做的一件事是尝试 Table.read(file, format='ascii', data_start=2, guess=False)

解决方案是将 sep 包含在 data = pd.read_table() 中,这样它会将每一列保持为单独的列,并将 sep 的类型指定为 "\s+"

n=10
li=[]
for i in range(0,n):
    file = "training_data/%s.dat"%i # This way I can call each file automatically 
    data = pd.read_table(file, sep="\s+") # I convert the file to pandas compatible
    li.append(data) # I add the file into the empty list above
# now I have my list ready so I compress it into 1 file
all_data = pd.concat(li)