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 ĐọcBàn luậnXem 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 Python3Hã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_name 0Output import pandas as pd Link
of the CSV file used: link
Python3Hã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_name 0Output import pandas as pd Link of the CSV file used: link
Python3Hã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_name 0
Output:
Làm cách nào để kéo một cột cụ thể 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 vẽ các cột cụ thể trong Python?
Để vẽ một cột cụ thể, hãy sử dụng phương thức lựa chọn của hướng dẫn dữ liệu tập hợp con kết hợp với phương thức lô ().Do đó, phương thức cốt truyện () hoạt động trên cả sê -ri và DataFrame.use the selection method of the subset data tutorial in combination with the plot() method. Hence, the plot() method works on both Series and DataFrame .
Làm cách nào để đọc một cột cụ thể trong tệp CSV trong Python?
Python3.Trong phương thức này, chúng tôi sẽ nhập thư viện CSV và mở tệp ở chế độ đọc, sau đó chúng tôi sẽ sử dụng hàm dictreader () để đọc dữ liệu của tệp CSV.Hàm này giống như một người đọc thông thường, nhưng nó ánh xạ thông tin đến một từ điển có các khóa được đưa ra bởi các tên cột và tất cả các giá trị dưới dạng các khóa.use the DictReader() function to read the data of the CSV file. This function is like a regular reader, but it maps the information to a dictionary whose keys are given by the column names and all the values as keys.
Làm cách nào để nhập một cột cụ thể trong Excel từ Python?
Làm cách nào để trích xuất các cột cụ thể trong Excel bằng Python ?.. Nhập Gandas dưới dạng PD .. Nhập Numpy dưới dạng NP .. file_loc = "path.xlsx". df = pd.read_excel (file_loc, index_col = none, na_values = ['na'], usecols = "a, c: aa"). print(df). |