How to add the new Column in DataFrame: - If we want to add the new ...

HOME PAGE

How to add the new Column in DataFrame: -

If we want to add the new column in DataFrame then new column can be added in two forms i.e. either we can append OR insert the column in DataFrame.

1. How to append the column in DataFrame: - if we want to append the column in DataFrame then we will use [ ] notation to append the column in DataFrame. Appending of column in DataFrame means to add the column at the end of DataFrame.

Syntax to append column in DataFrame using [ ] notation: -

Column label is user defined , It means user can give any name to newly created column.

DataFrame_object[ column_label ]= values

Notes : -values can be in List , scalar , array , Series format etc.

Example 1 to append column in DataFrame: Add new column `points' with values [ 4, 7, 8]

OUTPUT: -

New Column points will be added in DataFrame with values [4,7,8]

Example 2 to append column in DataFrame: Add new column `score1' with values [ 10,15,20]

OUTPUT: -

New Column score1 will be added in DataFrame with values [10,15,20]

Example 3 to append column in DataFrame with scalar value: Add new column `equalpoints' with scalar value 10.

OUTPUT: -

New Column equalpoints will be 10 added in DataFrame with scalar value

for whole column.

2. How to insert the column in DataFrame: - To insert the column in DataFrame means to add or insert the column at specific index position of DataFrme. To insert the column in DataFrame at specific index we will use insert() function of DataFrame.

Syntax to use the insert() function to column in DataFrame: -

Index Position where we want to insert column within DataFrame.

DataFrame_object . insert(loc , column_label , values)

Name of column i.e. User defined

Values we want to insert in

Column , can be scalar , list or in

any other format.

Example 1 to insert column in DataFrame using insert() function: Add new column Admno at index 1 with Values [1001,1002,1003]

OUTPUT: -

New Column Admno will be added in DataFrame with values [1001 ,1002 , 1003] at index 1.

Example 2 to insert column in DataFrame using insert() function: Add new column city at index 2 with Values [`jammu','pune','delhi']

OUTPUT: -

New Column city will be added in DataFrame with values [`jammu','pune','delhi'] at

index 2

How to add the row in DataFrame: if we want to add the row in DataFrame then we can use .loc method. And row will be added at the end of DataFrame.

Syntax to use the .loc method to add row in DataFrame: -

Column label is user defined , It means user can give any name to newly created column.

DataFrame_object . loc [ row _label ] = values Notes : -values can be in List , scalar , array , Series format etc.

Example 1 to add new row in DataFrame using .loc : Add new row `stu4' with values [4, `jatin' , 600] in DataFrame.

Row Label

Row values

OUTPUT: -

New Row `stu4' with values

Will be added in DataFrame.

Example 2 to add new row in DataFrame using .loc : Add new row `student4' with values [40 , `Lalit' , 650] in DataFrame.

Row Label

Row values

OUTPUT: -

New Row `student4' with values Will be added in DataFrame.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download