Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»Uncategorized»How to Make the First Column an Index in Python
    Uncategorized

    How to Make the First Column an Index in Python

    March 11, 2023No Comments6 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    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:

  • Series
  • DataFrames
  • The Series can represent the data in a one-dimensional arrangement like an array or a list.
  • A DataFrame, on the other hand, is a multi-dimensional representation of data like a table. It is simply an array of Series.
  • Here is a basic programme example:

    1. import pandas as p  
    2. name = p. Series ([“Raghav”, “Charan”, “Santhosh”])  
    3. roll = p. Series ([301, 202, 103])  
    4. branch = p. Series ([“ECE”, “EEE”, “CSE”])  
    5. age = p. Series ([19, 18, 19])  
    6. dataframe = p. DataFrame ({‘name’: name, ‘roll’: roll, ‘branch’: branch, ‘age’: age})  
    7. 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.

  • The entry into a data frame is taken as a key: value pair.
  • The key represents the column name, and the value represents the data in the column.
  • It creates an index by default from 0 to the number of rows-1 to represent the rows in the tabular representation.
  • The programmer can set any column as an index rather than just 0 to n. This tutorial explains how to do that.
  • 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:

    1. DataFrame. set_index (columns, drop = True, append = False, in place = False, verify_integrity = False)lass=“codeblock”><textarea name=“code” class=“python”>  

  • columns: List of columns we want to set as the index.
  • drop: If set False, after it is used as an index, the column is specified again inside the table like a regular column. Hence, it is set to True by default.
  • append: If set True, the column will be appended to the existing index rather than being the only index of the table. It is set False by default.
  • inplace: If set True, it determines whether to use a new data frame or to update the current data frame. It is set False by default.
  • verify_integrity: If set True, it will check for duplicates in the index and returns a ValueError, if any. It is set False by default.
  • Example:

    1. import pandas as p  
    2. Studentdata = {  
    3.         “Names”: [“Raghav”, “Charan”, “Santosh”],  
    4.         “Branch”: [“ECE”, “B-Arch”, “AIML”],  
    5.         “Age”: [19, 18, 19],  
    6.         “CGPA”: [9.1, 9.4, 9.6]  
    7.         }  
    8. dataframe = p. DataFrame (Student data, index = [“Student1”, “Student2”, “Student3”])  
    9. print (“original dataframe: “)  
    10. print (dataframe)  
    11. dataframe = dataframe. set_index ([‘Branch’])  
    12. print (“\nBranch column as index to the data frame: “)  
    13. print (dataframe)  
    14. print (“\nNames and Age columns as an index to the data frame: “)  
    15. dataframe = dataframe. set_index ([‘Names’, ‘Age’])  
    16. 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.

  • For working with .csv files:
  • With pandas, a programmer may manipulate any file type. For example, to manipulate CSV files:

  • A CSV file is a text file that stores the data in the form of tables with values separated by commas.
  • Program:

    1. import pandas as p  
    2. Employeedata = {  
    3.         “Names”: [“Sudha”, “Harini”, “Venkat”],  
    4.         “Branch”: [“HR”, “Developer”, “Sales”],  
    5.         “Age”: [44, 23, 44],  
    6.         “Salary”: [112000, 94000, 122000],  
    7.         “Experience (yrs)”: [8, 2, 8]  
    8.         }  
    9. dataframe = p. DataFrame (Employeedata)  
    10. print (dataframe)  
    11. dataframe. to_csv  (‘samplefile.csv’, index = False)  
    12. dataframe = p. read_csv (‘samplefile.csv’, index_col = 0)  
    13. 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:

    1. 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:

    Learn Python free Python Code Python Course Free download python coursefree Courses Download Python Language
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Next Article Python Sets

    Related Posts

    python

    Plot With Pandas Python Data Visualization Basics

    March 27, 2023
    python

    Defining and Calling Python Functions

    March 27, 2023
    python

    BreadthFirst Search in Python

    March 27, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.