V-Lab @ ANDC

Creation of DataFrame

Aim

To create an empty data frame and add new columns in it (prime number and its square)

Theory

A DataFrame is similar to an in-memory spreadsheet. Like a spreadsheet:

  • A DataFrame stores data in cells.
  • A DataFrame has named columns (usually) and numbered rows.

Features of DataFrame

  • Potentially columns are of different types
  • Size - Mutable
  • Labeled axes (rows and columns)
  • Can Perform Arithmetic operations on rows and columns

pandas.DataFrame

A pandas DataFrame can be created using the following constructor -

pandas.DataFrame( data, index, columns, dtype, copy)

The parameters of the constructor are as follows -

  1. data:- data takes various forms like ndarray, series, map, lists, dict, constants and also another DataFrame.
  2. index:- For the row labels, the Index to be used for the resulting frame is Optional Default np.arange(n) if no index is passed.
  3. columns:- For column labels, the optional default syntax is - np.arange(n). This is only true if no index is passed.
  4. Dtype:- Data type of each column.
  5. copy:- This command is used for copying of data, if the default is False.

Create an Empty DataFrame

A basic DataFrame, which can be created is an Empty Dataframe.
import pandas as pd
df =pd.DataFrame()
print(df)

Column Addition

We will understand this by adding column to an existing data frame.
import pandas as pd 
df =pd.DataFrame() # Adding a column to an existing DataFrame df['Rollno']=[1,2,3,4,5] df['Name']=['Kunal','Ram','Sahil','Preeti','Raj'] print(df)

output

Rollno Name
0 1 kunal
1 2 Ram
2 3 sahil
3 4 preeti
4 5 Raj


Procedure

  1. Press start to start the experiment
  2. Press next to see the execution of the code
  3. Relevant line in the code is shown here
  4. The output of the code shown in the right

Manual

In this experiment you will understand how to create a data frame and add columns to the data frame.

Practice

Instructions:

Just click the next button to see which element goes to which position.

On runing: pandas.DataFrame([[1,2,3],[4,5,6],[7,8,9]], [0,1,2],['One','Two','Three'] )

[

[

1

,

2

,

3

]

,

[

4

,

5

,

6

]

,

[

7

,

8

,

9

]

]

Name of Columns
Row Index 'One' 'Two' 'Three'
0 1 2 3
1 4 5 6
2 7 8 9

Observation

The start of row 0

The element 1 of sub-list 1 is assigned to [ 0 , 0 ] in the DataFrame

The element 2 of sub-list 1 is assigned to [ 0 , 1 ] in the DataFrame

The element 3 of sub-list 2 is assigned to [ 0 , 2 ] in the DataFrame

The end of row 0

The start of row 1

The element 4 of sub-list 2 is assigned to [ 1 , 0 ] in the DataFrame

The element 5 of sub-list 2 is assigned to [ 1 , 1 ] in the DataFrame

The element 6 of sub-list 3 is assigned to [ 1 , 2 ] in the DataFrame

The end of row 1

The start of row 2

The element 7 of sub-list 3 is assigned to [ 2 , 0 ] in the DataFrame

The element 8 of sub-list 3 is assigned to [ 2 , 1 ] in the DataFrame

The element 9 of sub-list 4 is assigned to [ 2 , 2 ] in the DataFrame

The end of row 2

The end of row 3

Quiz


Q1. Which of the following python libraries are used to create a dataframe:-


  • matplotlib
  • numpy
  • pandas

Q2. Which of the following is the correct syntax to create an empty dataframe :-

  • pd.dataframe(data)
  • pd.df(data)
  • pd.DataFrame(date)

Q3. What is the correct syntax to add a column in a dataframe :-

  • df["Name"] = np.arange(10)
  • df.Name = np.arange(10)
  • df[Name] = np.arange(10)

Q4. What is the output of the following code:-
import pandas as pd
Df = pd.DataFrame()
Df["Numbers"] = np.arange(3)
Df["Square"] = Df["Numbers"]*Df["Numbers"]
print(Df)
                        

  • Number Square
    0 1 1
    1 2 4
    2 3 9
  • Number Square
    0 1 1
    1 2 4
    2 3 9
  • Square Number
    0 1 1
    1 2 4
    2 3 9

Q5. We can use the __ method to merge two DataFrames

  1. join()
  2. drop()
  3. append()

Team

Mr. Vivek Sharma, B.Sc Physical Sciences with Computer Science, II year,
Ms. Palak Sharma, B.Sc Physical Sciences with Computer Science, II year

Mentors:
Prof. Sharanjit Kaur,
Ms. Gunjan Rani

Text Copied