Hướng dẫn how do i import a specific column in python? - làm cách nào để nhập một cột cụ thể trong python?

Tôi có một tệp CSV từ trang web này. Tôi muốn đọc một số cột trong tệp đã tải xuống (phiên bản CSV có thể được tải xuống ở góc trên bên phải).

Giả sử tôi muốn có 2 cột:

  • 59 mà trong tiêu đề là star_name
  • 60 mà trong tiêu đề là ra.

Tuy nhiên, vì một số lý do, các tác giả của trang web đôi khi quyết định di chuyển các cột xung quanh.

Cuối cùng, tôi muốn một cái gì đó như thế này, hãy nhớ rằng các giá trị có thể bị thiếu.

data = #read data in a clever way
names = data['star_name']
ras = data['ra']

Điều này sẽ ngăn chặn chương trình của tôi bị trục trặc khi các cột được thay đổi trở lại trong tương lai, nếu chúng giữ tên chính xác.

Cho đến bây giờ tôi đã thử nhiều cách khác nhau bằng cách sử dụng mô -đun csv và mô -đun pandas. Cả hai mà không có bất kỳ may mắn.

Chỉnh sửa (Đã thêm hai dòng + Tiêu đề của DataFile của tôi. Xin lỗi, nhưng nó cực kỳ dài.)

# 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,,

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc pandas.read_csv()method. We will pass the first parameter as the CSV file and the second parameter the list of specific columns in the keyword usecols. It will return the data of the CSV file of specific columns.

    Bàn luận 1: Link of the CSV file used: link

    Python3

    Hãy cho chúng tôi xem cách đọc các cột cụ thể của tệp CSV bằng gấu trúc. Điều này có thể được thực hiện với sự trợ giúp của phương thức thePandas.Read_csv (). Chúng tôi sẽ vượt qua tham số đầu tiên dưới dạng tệp CSV và tham số thứ hai danh sách các cột cụ thể trong từ khóa usecols. Nó sẽ trả về dữ liệu của tệp CSV của các cột cụ thể.

    Ví dụ & NBSP; 1: Liên kết của tệp CSV được sử dụng: Liên kết

    # 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,,
    
    9star_name0

    Output   
     

    Hướng dẫn how do i import a specific column in python? - làm cách nào để nhập một cột cụ thể trong python?

    import pandas as pdLink of the CSV file used: link
     

    Python3

    Hãy cho chúng tôi xem cách đọc các cột cụ thể của tệp CSV bằng gấu trúc. Điều này có thể được thực hiện với sự trợ giúp của phương thức thePandas.Read_csv (). Chúng tôi sẽ vượt qua tham số đầu tiên dưới dạng tệp CSV và tham số thứ hai danh sách các cột cụ thể trong từ khóa usecols. Nó sẽ trả về dữ liệu của tệp CSV của các cột cụ thể.

    Ví dụ & NBSP; 1: Liên kết của tệp CSV được sử dụng: Liên kết

    # 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,,
    
    9star_name0

    Output   
     

    Hướng dẫn how do i import a specific column in python? - làm cách nào để nhập một cột cụ thể trong python?

    import pandas as pdLink of the CSV file used: link

    Python3

    Hãy cho chúng tôi xem cách đọc các cột cụ thể của tệp CSV bằng gấu trúc. Điều này có thể được thực hiện với sự trợ giúp của phương thức thePandas.Read_csv (). Chúng tôi sẽ vượt qua tham số đầu tiên dưới dạng tệp CSV và tham số thứ hai danh sách các cột cụ thể trong từ khóa usecols. Nó sẽ trả về dữ liệu của tệp CSV của các cột cụ thể.

    Ví dụ & NBSP; 1: Liên kết của tệp CSV được sử dụng: Liên kết

    # 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,,
    
    9star_name0

    Output: 
     

    Hướng dẫn how do i import a specific column in python? - làm cách nào để nhập một cột cụ thể trong python?


    Làm thế nào để bạn nhập một cột trong Python?

    Các bước để nhập tệp CSV vào Python bằng Pandas..
    Bước 1: Chụp đường dẫn tệp.Đầu tiên, chụp toàn bộ đường dẫn nơi tệp CSV của bạn được lưu trữ.....
    Bước 2: Áp dụng mã Python.....
    Bước 3: Chạy mã.....
    Bước tùy chọn: Chọn tập hợp con của các cột ..

    Làm thế nào để bạn có được một cột cụ thể trong Python?

    Để chọn một cột duy nhất, hãy sử dụng dấu ngoặc vuông [] với tên cột của cột quan tâm.use square brackets [] with the column name of the column of interest.

    Làm cách nào để nhập chỉ một số cột từ tệp CSV?

    Phương pháp 1: Sử dụng hàm read.table ().Trong phương thức này chỉ nhập các cột đã chọn của dữ liệu tệp CSV, người dùng cần gọi đọc.Hàm bảng (), là hàm được xây dựng của ngôn ngữ lập trình R, sau đó chuyển cột đã chọn trong các đối số của nó để nhập các cột cụ thể từ dữ liệu.Using read. table() function. In this method of only importing the selected columns of the CSV file data, the user needs to call the read. table() function, which is an in-built function of R programming language, and then passes the selected column in its arguments to import particular columns from the data.

    Làm cách nào để nhập một cột cụ thể trong Excel từ Python?

    Cách tiếp cận :..
    Nhập chương trình gấu trúc ..
    Tạo một khung dữ liệu ..
    Lưu trữ dữ liệu Excel vào DataFrame ..
    Kiểm tra cột cụ thể và hiển thị với hàm Head () ..