Hướng dẫn how do i read a specific column in a dataframe in python? - làm cách nào để đọc một cột cụ thể trong khung dữ liệu 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 read a specific column in a dataframe in python? - làm cách nào để đọc một cột cụ thể trong khung dữ liệu 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 read a specific column in a dataframe in python? - làm cách nào để đọc một cột cụ thể trong khung dữ liệu 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 read a specific column in a dataframe in python? - làm cách nào để đọc một cột cụ thể trong khung dữ liệu trong python?


    Làm thế nào để bạn chọn một cột cụ thể từ một bộ dữ liệu trong Python?

    Chọn các cột dựa trên tên của chúng Đây là cách cơ bản nhất để chọn một cột duy nhất từ ​​DataFrame, chỉ cần đặt tên chuỗi của cột vào dấu ngoặc. Trả lại một loạt gấu trúc. Vượt qua danh sách trong dấu ngoặc cho phép bạn chọn nhiều cột cùng một lúc.put the string name of the column in brackets. Returns a pandas series. Passing a list in the brackets lets you select multiple columns at the same time.

    Làm thế nào để bạn nhận được một giá trị cụ thể từ một cột trong DataFrame?

    loc [] để có được giá trị ô theo tên cột.Trong gấu trúc, thuộc tính dataFrame.loc [] được sử dụng để có được giá trị ô cụ thể theo tên hàng & nhãn (tên cột).Bên dưới tất cả các ví dụ trả về một giá trị ô từ nhãn R4 và cột thời lượng (cột thứ 3).DataFrame. loc[] property is used to get a specific cell value by row & label name(column name). Below all examples return a cell value from the row label r4 and Duration column (3rd column).

    Làm cách nào để đọc một cột từ tệp CSV trong Python?

    Điều này có thể được thực hiện với sự trợ giúp của phương thức pandas.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ể.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.

    Làm thế nào để tôi chỉ thấy một cột trong gấu trúc?

    Trong Pandas, chúng ta có thể chọn một cột duy nhất chỉ bằng cách sử dụng toán tử chỉ mục [], nhưng không có danh sách làm đối số.Tuy nhiên, đối tượng kết quả là một loạt gấu trúc thay vì gấu trúc DataFrame.Ví dụ: nếu chúng ta sử dụng DF ['A'], chúng ta sẽ chọn cột đơn làm đối tượng Pandas Series.using the index operator [], but without list as argument. However, the resulting object is a Pandas series instead of Pandas Dataframe. For example, if we use df['A'], we would have selected the single column as Pandas Series object.