Question
How to add an Index, row, or column to a Pandas DataFrame?

Answer

Adding an Index to a DataFrame:

Pandas allow adding the inputs to the index argument if you create a DataFrame. It will make sure that you have the desired index. If you don’t specify inputs, the DataFrame contains, by default, a numerically valued index that starts with 0 and ends on the last row of the DataFrame.

Adding Rows to a DataFrame:

We can use .loc, iloc, and ix to insert the rows in the DataFrame.

The loc basically works for the labels of our index. It can be understood as if we insert in loc[4], which means we are looking for that values of DataFrame that have an index labeled 4.

The iloc basically works for the positions in the index. It can be understood as if we insert in iloc[4], which means we are looking for the values of DataFrame that are present at index '4`.

The ix is a complex case because if the index is integerbased, we pass a label to ix. The ix[4] means that we are looking in the DataFrame for those values that have an index labeled 4. However, if the index is not only integer-based, ix will deal with the positions as iloc.

Adding Columns to a DataFrame

If we want to add the column to the DataFrame, we can easily follow the same procedure as adding an index to the DataFrame by using loc or iloc.

Need a full question paper?

Generate a complete, print-ready paper with questions like this in minutes — across 16+ boards, with answer keys.

Start Generating Free

Similar questions

Write a script to create a list of 10 random floating point numbers and then
(a) Display this list of numbers
(b) Find the largest and smallest numbers present in the list (without using max() and min() functions)
Write a menu driven python program using function InQ(), DelQ() and DisplayQ() to implement the Queue. The program will store the name of the books.
Write a function readall() the record from the binary file “student” and display all the records
WAP to find the sum of first n terms of the following series:
$1+\frac{x^3}{3 !}+\frac{x^5}{5 !}+\frac{x^7}{7 !} \ldots$
Write a function delrecord() to delete the record form the binary file “student”. The record should be deleted on the basis of student number.
Write SQL commands for the following:

Table: TEACHER

TID NAME AGE DEPT DATEOFJOIN SAL SEX
T118 Navin 40 Computer 2010-01-10 12000 M
T107 Chetna 37 History 2008-03-24 20000 F
T105 Sandeep 46 Maths 2006-12-12 30000 M
T110 Sangeeta 35 History 2010-07-01 25000 F
T101 Rudransh 42 Maths 2004-09-05 40000 M
T121 Neeraj 38 Physics 2011-04-01 28000 M

(i) To show information about the teachers of the history department.

(ii) To list the names of teachers earning a salary between 20000 and 30000.

(iii) To count the number of male teachers.

(iv) Display gender wise total number of teachers.

(v) To list the name and age of teachers of female teachers in descending order of date of join.

(vi) Increase the salary by 10% for Maths departments.

( vii) To delete the record of teacher Neeraj.

Write python program using function to find the sum of first n terms of the following series:

where n and x have to be input from the user. The function should have two parameter n and x and return the sum of the series.

Answer the questions related to pandas Series which will perform:

(i) To import the pandas

(ii) To create the series from the list

(iii) To print List.

(iv) To print the index of the series.

(v) To print the values of the series.

Expertia Professional Global (EPG) is an online corporate training provider company for IT related courses. The company is setting up their new campus in Mumbai. You as a network expert have to study the physical locations of various buildings and the number of computers to be installed. In the planning phase, provide the best possible answers for the queries (a) to (d) raised by them.

Building to Building distances (in Mtrs.)

FROMToDistance
Administrative BuildingFinance Building60
Administrative BuildingFaculty Studio building120
Finance BuildingFaculty Studio building70

Number of computers in each of the blocks/Center is as follows:

Administrative Building20
Finance Building40
Faculty Studio building120

(a) Suggest the most appropriate building, where EPG should plan to install the server.

(b) Suggest the most appropriate building to building cable layout to connect all three buildings for efficient communication.

(c) Which type of network out of the following is formed by connecting the computers of these three buildings?

LAN, MAN, WAN

(d) Which wireless channel out of the following should be opted by EPG to connect to students of all over the world?

Infrared, Microwave, Satellite

Write a menu driven program in python to implement the various method related to insertion and deletion in the list.