我有10份文件astropy.table.桌子.Table文件类型,全部由相同的六列(mjd、filter、flux、flux\u error、zp、zpsys)组成,但长度不同。 首先我想把每个文件转换成1.core.frame框架.DataFrame文件类型,以便我可以将它们全部添加到一个列表中并使用pd.concat公司函数将所有10个文件转换为1个大文件1.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列,这使得我无法完成剩下的工作。你知道吗
当我检查所有数据的形状时,我得到(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
解决方案是在数据中包含sep=pd.read\表()以便将每列保持为单独的列,并将sep的类型指定为“\s+”
可能是
Table.read()
无法猜测数据的格式/分隔符。我可以使用Table.read(file, format='ascii', data_start=2)
将包含的示例(文件0中的数据)读入一个包含6列的表中,但我不确定是否正确捕获了空格。你知道吗我怀疑文件0中的示例数据不是您正在读取的数据,因为如果没有
data_start=2
,该文件将显示第1行为“float64 str4 float64 float64 str2”。你知道吗你可以做的一件事就是试试
Table.read(file, format='ascii', data_start=2, guess=False)
。你知道吗相关问题 更多 >
编程相关推荐