Python Tutorial: Pandas DataFrame-Add Column(s)

Friday, 4 September 2020

Pandas DataFrame-Add Column(s)

 Adding columns into DataFrame

There are five ways to add a new column into a DataFrame. These are indexing, loc, assign(), insert() and concat(). The concat() function can Adding Columns and Rows.

A Table Student Information

Name

Position

City

Age

Sex

Aakash

Manager

Delhi

35

M

Sunakshi

Programmer

Mumbai

37

F

Amitabh

Manager

Kanpur

33

M

Madhuri

Programmer

Mumbai

40

F

Ashish

Manager

Kanpur

27

M

Akshay

Programmer

Kanpur

34

M

Preeti

Programmer

Delhi

26

F

Govinda

Manager

Delhi

30

M

 

>>> import pandas as pd

>>> Student={'Name':['Aakash','Sunakshi','Amitabh','Madhuri','Ashish','Akshay','Preeti','Govinda']}

>>> df=pd.DataFrame(Student,columns=['Name'])

>>> df

 

Output

      Name

0    Aakash

1  Sunakshi

2   Amitabh

3   Madhuri

4    Ashish

5    Akshay

6    Preeti

7   Govinda

 

Method1 : Using Index

The DataFrame df is now integer index based. To add a second column i.e. Position using indexing process,

>>> df['Position']=['Manager','Programmer','Manager','Programmer','Manager','Programmer','Programmer','Manager']

>>> df

Method 2 : Using .loc

>>> df.loc[:,'City']=['Delhi','Mumbai','Kanpur','Mumbai','Kanpur','Kanpur','Delhi','Delhi']

>>> df

Method3: Using assign() function

.loc has two limitations that it mutates the DataFrame in-place and it can’t be used with method chaining. If that’s a problem for you, use assign() function. The assign() function in Python, assigns the new column to the existing DataFrame. The Syntax is

DataFrame=DataFrame.assign(List)

Here,

The DataFrame on left side of assignment sign assigns a new DataFrame along with a new list (i.e. List).

The DataFrame on right side of assignment sign can assigns a list temporarily.

If the both the DataFrame name is same then the new DataFrame will hold a new column as the List.

 >>> df=df.assign(Age=[35,37,33,40,27,34,26,30])

>>> df

Method 4: Using Insert() function

The insert() function adds a column at the column index position. In a DataFrame, the first column is started from 0,1,2,3,4….. and so on. To add a column using insert() function.

Syntax

DataFrameName.insert(loc, column, value, allow_duplicates = False)

Parameters:

loc:

loc is an integer which is the location of column where we want to insert new column. This will shift the existing column at that position to the right.

column:

column is a string which is name of column to be inserted.

value:

value is simply the value to be inserted. It can be int, string, float or anything or even series / List of values. Providing only one value will set the same value for all rows.

allow_duplicates :

allow_duplicates is a boolean value which checks if column with same name already exists or not.

Example

>>> idx=4

>>> Sex=['M','F','M','F','M','M','F','M']

>>> df.insert(loc=idx,column="Sex",value=Sex)

>>> df

Output:

No comments:

Post a Comment