[英]Trying to understand pandas dataframes
I have a space-delimited .dat
file, for which the first few lines look like this: 我有一个以空格分隔的
.dat
文件,其前几行如下所示:
1 SDSSJ000005.95+145310.1 2.49900 * 0.000e+00 0.00 NA -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 0.000 0.000 NONE
4 SDSSJ000009.27+020621.9 1.43200 UvS 0.000e+00 0.00 NA -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 0.000 0.000 NONE
5 SDSSJ000009.38+135618.4 2.23900 QSO 0.000e+00 0.00 NA -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 0.000 0.000 NONE
6 SDSSJ000011.37+150335.7 2.18000 * 0.000e+00 0.00 NA -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 0.000 0.000 NONE
11 SDSSJ000030.64-064100.0 2.60600 QSO 0.000e+00 0.00 NA -999.000 -999.000 -999.000 -999.000 15.460 -999.000 -999.000 -999.000 -999.000 23.342 56.211 UV
15 SDSSJ000033.05+114049.6 0.73000 UvS 0.000e+00 0.00 NA -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 -999.000 0.000 0.000 NONE
27 LBQS2358+0038 0.95000 QSO 0.000e+00 0.00 NA 17.342 18.483 18.203 17.825 -999.000 -999.000 -999.000 -999.000 -999.000 23.301 56.572 UV
They're astronomical measurements, and there are 29008 lines in the file. 它们是天文测量,文件中有29008行。 When I read the file with
当我用
import pandas as pd
data = pd.read_csv('todo.dat', sep = ' ',
names = ['no', 'NED', 'z', 'obj_type','S_21', 'power',
'SI_flag','U_mag', 'B_mag', 'V_mag', 'R_mag',
'K_mag', 'W1_mag', 'W2_mag', 'W3_mag', 'W4_mag',
'L_UV', 'Q', 'flag_uv'])
the dataframe shows [29008 rows x 19 columns]
. 数据
[29008 rows x 19 columns]
显示[29008 rows x 19 columns]
。 I want to organise the data based on the column headed z
(which is the third column -- index 2). 我想基于标题为
z
的列组织数据(这是第三列-索引2)。 Adding index_col='z'
to the read_csv
call gives me a KeyError: 'z'
error, but using index_col = 2 doesn't give me an error. 在
read_csv
调用中添加index_col='z'
会给我一个read_csv
KeyError: 'z'
错误,但是使用index_col = 2不会给我一个错误。 I thought pandas labelled the headers like a dictionary, so 'z'
should be the key in the dictionary for that column. 我以为大熊猫将标题标为字典,因此
'z'
应该是该列在字典中的键。 So why do I get an error when I refer to index 2 as 'z'? 那么,当我将索引2称为“ z”时为什么会出现错误?
To me this seems buggy, and possibly related to this issue . 对我来说,这似乎是有问题的 ,可能与这个问题有关 。 An easy work around is just to use
set_index
afterhand: 一个简单的解决办法就是使用
set_index
afterhand:
data = pd.read_csv('todo.dat', sep = ' ',
names = ['no', 'NED', 'z', 'obj_type','S_21', 'power',
'SI_flag','U_mag', 'B_mag', 'V_mag', 'R_mag',
'K_mag', 'W1_mag', 'W2_mag', 'W3_mag', 'W4_mag',
'L_UV', 'Q', 'flag_uv']).set_index('z')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.