使用pandas或其他python模块读取特定列

Dan*_*sen 49 python csv pandas

我在这个网页上有一个csv文件.我想阅读下载文件中的一些列(可以在右上角下载csv版本).

假设我想要2列:

  • 标题中的59是 star_name
  • 标题中的60是ra.

但是,出于某种原因,网页的作者有时会决定移动列.

最后我想要这样的东西,记住价值可能会丢失.

data = #read data in a clever way
names = data['star_name']
ras = data['ra']
Run Code Online (Sandbox Code Playgroud)

如果他们保持名称正确,这将阻止我的程序在将来再次更改列时出现故障.

到目前为止,我已经尝试了各种方式使用csv模块和怨恨pandas模块.两个都没有运气.

编辑(添加两行+我的数据文件的标题.抱歉,但它非常长.)

# name, mass, mass_error_min, mass_error_max, radius, radius_error_min, radius_error_max, orbital_period, orbital_period_err_min, orbital_period_err_max, semi_major_axis, semi_major_axis_error_min, semi_major_axis_error_max, eccentricity, eccentricity_error_min, eccentricity_error_max, angular_distance, inclination, inclination_error_min, inclination_error_max, tzero_tr, tzero_tr_error_min, tzero_tr_error_max, tzero_tr_sec, tzero_tr_sec_error_min, tzero_tr_sec_error_max, lambda_angle, lambda_angle_error_min, lambda_angle_error_max, impact_parameter, impact_parameter_error_min, impact_parameter_error_max, tzero_vr, tzero_vr_error_min, tzero_vr_error_max, K, K_error_min, K_error_max, temp_calculated, temp_measured, hot_point_lon, albedo, albedo_error_min, albedo_error_max, log_g, publication_status, discovered, updated, omega, omega_error_min, omega_error_max, tperi, tperi_error_min, tperi_error_max, detection_type, mass_detection_type, radius_detection_type, alternate_names, molecules, star_name, ra, dec, mag_v, mag_i, mag_j, mag_h, mag_k, star_distance, star_metallicity, star_mass, star_radius, star_sp_type, star_age, star_teff, star_detected_disc, star_magnetic_field
11 Com b,19.4,1.5,1.5,,,,326.03,0.32,0.32,1.29,0.05,0.05,0.231,0.005,0.005,0.011664,,,,,,,,,,,,,,,,,,,,,,,,,,,,,1,2008,2011-12-23,94.8,1.5,1.5,2452899.6,1.6,1.6,Radial Velocity,,,,,11 Com,185.1791667,17.7927778,4.74,,,,,110.6,-0.35,2.7,19.0,G8 III,,4742.0,,
11 UMi b,10.5,2.47,2.47,,,,516.22,3.25,3.25,1.54,0.07,0.07,0.08,0.03,0.03,0.012887,,,,,,,,,,,,,,,,,,,,,,,,,,,,,1,2009,2009-08-13,117.63,21.06,21.06,2452861.05,2.06,2.06,Radial Velocity,,,,,11 UMi,229.275,71.8238889,5.02,,,,,119.5,0.04,1.8,24.08,K4III,1.56,4340.0,,
Run Code Online (Sandbox Code Playgroud)

Dan*_*sen 98

一个简单的方法是使用这样的pandas库.

import pandas as pd
fields = ['star_name', 'ra']

df = pd.read_csv('data.csv', skipinitialspace=True, usecols=fields)
# See the keys
print df.keys()
# See content in 'star_name'
print df.star_name
Run Code Online (Sandbox Code Playgroud)

这里的问题是skipinitialspace删除标题中的空格.所以'star_name'变成'star_name'

  • 列也可以用数字指定,例如 `usecols=[0, 1]` 将只读取前两列。 (4认同)

Dec*_*ist 21

根据最新的 Pandas 文档,您可以读取一个 csv 文件,只选择您想要读取的列。

import pandas as pd

df = pd.read_csv('some_data.csv', usecols = ['col1','col2'], low_memory = True)
Run Code Online (Sandbox Code Playgroud)

这里我们使用usecolswhich 只读取数据框中选定的列。

我们正在使用,low_memory以便我们在内部以块的形式处理文件。

  • 如果您希望以块的形式解析文件,则应将“low_memory”保留为其默认值“True”。[参见文档](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html)。 (2认同)

Zub*_*yer 7

以上答案适用于python2。所以对于 python 3 用户我给出这个答案。您可以使用下面的代码:

import pandas as pd
fields = ['star_name', 'ra']

df = pd.read_csv('data.csv', skipinitialspace=True, usecols=fields)
# See the keys
print(df.keys())
# See content in 'star_name'
print(df.star_name)
Run Code Online (Sandbox Code Playgroud)


frp*_*han 5

以不同的方式解决了上述问题,虽然我会读取整个 csv 文件,但会调整显示部分以仅显示所需的内容。

import pandas as pd

df = pd.read_csv('data.csv', skipinitialspace=True)
print df[['star_name', 'ra']]
Run Code Online (Sandbox Code Playgroud)

这可以帮助在某些场景中学习基础知识并根据数据框中的列过滤数据。

  • 关于答案。在这种情况下,pandas 将首先将整个数据集加载到内存中。如果文件很大,这可能是一个问题。 (2认同)