How to Make the First Column in Python Foundation an Index A concise summary of the prerequisite knowledge:Pandas is one of the most popular Python libraries. It is frequently used in several machine learning and data analysis applications. The programmer may produce, read, and modify vast volumes of data and deal with any file type using Pandas. ‘Pandas’ has several ML techniques that may be used to massive data chunks to get the desired outcome.In Pandas, data may be organised in two ways:
Here is a basic programme example:
- import pandas as p
- name = p. Series ([“Raghav”, “Charan”, “Santhosh”])
- roll = p. Series ([301, 202, 103])
- branch = p. Series ([“ECE”, “EEE”, “CSE”])
- age = p. Series ([19, 18, 19])
- dataframe = p. DataFrame ({‘name’: name, ‘roll’: roll, ‘branch’: branch, ‘age’: age})
- print (dataframe)
Output:
name roll branch age 0 Raghav 301 ECE 19 1 Charan 202 EEE 18 2 Santhosh 103 CSE 19
Three distinct Series are constructed, including name, age, role, and branch. All of the information is then merged into a data frame to represent a table.
set index Method: With the plethora of easy methods and tools, we can set any column as an index using the simple “set index()” method.Syntax:
- DataFrame. set_index (columns, drop = True, append = False, in place = False, verify_integrity = False)lass=“codeblock”><textarea name=“code” class=“python”>
Example:
- import pandas as p
- Studentdata = {
- “Names”: [“Raghav”, “Charan”, “Santosh”],
- “Branch”: [“ECE”, “B-Arch”, “AIML”],
- “Age”: [19, 18, 19],
- “CGPA”: [9.1, 9.4, 9.6]
- }
- dataframe = p. DataFrame (Student data, index = [“Student1”, “Student2”, “Student3”])
- print (“original dataframe: “)
- print (dataframe)
- dataframe = dataframe. set_index ([‘Branch’])
- print (“\nBranch column as index to the data frame: “)
- print (dataframe)
- print (“\nNames and Age columns as an index to the data frame: “)
- dataframe = dataframe. set_index ([‘Names’, ‘Age’])
- print (dataframe)
Output:
Original dataframe: Names Branch Age CGPA Student1 Raghav ECE 19 9.1 Student2 Charan B-Arch 18 9.4 Student3 Santosh AIML 19 9.6 Branch column as the index to the data frame: Names Age CGPA Branch ECE Raghav 19 9.1 B-Arch Charan 18 9.4 AIML Santosh 19 9.6 Names and Age columns as the index to the data frame: CGPA Names Age Raghav 19 9.1 Charan 18 9.4 Santosh 19 9.6
In the first section of the code above, the column ‘Branch’ is added to the index. By default, once it is created an index, it is dropped from the database, since drop is set to True. So, when the columns ‘Names’ and ‘Age’ are created indices, the ‘Branch’ column is omitted in the subsequent section.
With pandas, a programmer may manipulate any file type. For example, to manipulate CSV files:
Program:
- import pandas as p
- Employeedata = {
- “Names”: [“Sudha”, “Harini”, “Venkat”],
- “Branch”: [“HR”, “Developer”, “Sales”],
- “Age”: [44, 23, 44],
- “Salary”: [112000, 94000, 122000],
- “Experience (yrs)”: [8, 2, 8]
- }
- dataframe = p. DataFrame (Employeedata)
- print (dataframe)
- dataframe. to_csv (‘samplefile.csv’, index = False)
- dataframe = p. read_csv (‘samplefile.csv’, index_col = 0)
- dataframe. head ()
Output:
Names Branch Age Salary Experience (yrs) 0 Sudha HR 44 112000 8 1 Harini Developer 23 94000 2 2 Venkat Sales 44 122000 8
Using.tocsv (, the data frame is transformed into a CSV file, and.read csv (is used to read the file.) In this dataframe, we are able to alter the index into whatever column we like, similar to a typical dataframe:
- dataframe. set_index (‘Names’)
As with a typical data frame, the CSV file’s data frame will be updated, and the index will become the ‘Names’ column. The file may be located and inspected in the python directory: