![]() |
A CSV (comma-separated values) file is a text file with comma-separated values. They are used in data science and machine learning to represent the dataset. They are used in business applications to manage large amounts of data. Information in the CSV file can be represented in a tabular form with rows and columns. The CSV file uses UTF-8 encoding.NULL or missing values are represented using an empty field or with “NaN”.It does not support macros, styling, formulas, or calculations. In this article, we will learn how to retrieve a specific element from a CSV file. A specific element in the CSV file is associated with the column name and the row number. To get the specific element, we can use an iterative method or the element index. Different languages, such as Python, Java, or Node.js are used to retrieve the specific element. How do I retrieve a specific element from a CSV file?To retrieve the specific element from the CSV file, we need to have information about the CSV file. Information like header contents and number of rows in the file. We can use an iterative method or the element index to retrieve the specific element. The CSV file used is StudentPerformance.CSV.Contents of the file are gender,race/ethnicity,parental level of education,lunch,test preparation course,math score,reading score,writing score Method 1: Using the Pandas library.Python supports different libraries; Pandas is one of them. Pandas Library supports different methods that are used for preprocessing data, handling missing data, slicing data, filtering, and indexing the data. Here are steps to retrieve the specific element: Step 1: Import pandas libraryIt is the initial step through which the pandas’ library is imported. Python
Step 2: Read the CSV file.In this step, the CSV file is read using the read_csv() method which is supported by pandas. The read file is stored in the DataFrame. File location should be always mentioned in the read_csv() method, it can also have different arguments. Python3
Output: gender race/ethnicity parental level of education lunch \ Step 3: Retrieve the specific elementIn this step specific element is retrieved based on the label. Pandas support the .loc attribute which uses label-based indexing on the DataFrame.Dataframe is a two-dimensional, tabular data structure in Pandas. Python3
Output: Element at 3+1=4th rows of gender column : male Initially, pandas library is imported and csv file is read. It is stored in a data frame and the .loc attribute is used to retrieve the gender of the index 3. The value of the 3 index is printed at last. Method 2: Defining a functionPython3
Retrieve A Specific ElementPython3
Output: Element at row 4, column 0: male ConclusionIn this article, we have used python libraries and defined a python function to retrieve a specific element from a CSV file. We have learned how to retrieve the specific element from the CSV file. |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |