TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python Pandas Add column to DataFrame columns

ython Pandas Add column to DataFrame columns with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PYTHON

Add a column to DataFrame Columns

We can add a new column to an existing DataFrame using different ways. For the demonstration, first, we have to write a code to read the existing file, which consists of some columns in a DataFrame.

import pandas as pd
aa = pd.read_csv("aa.csv")
aa.head()

The above code read the existing csv file and shows the data values column as the output.

Output

Name Hire Date Salary Leaves Remaining
0 John Idle 03/15/14 50000.0 10
1 Smith Gilliam 06/01/15 65000.0 8
2 Parker Chapman 05/12/14 45000.0 10
3 Jones Palin 11/01/13 70000.0 3
4 Terry Gilliam 08/12/14 48000.0 7
5 Michael Palin 05/23/13 66000.0 8

Add new columns to a DataFrame using [] operator

If we want to add any new column at the end of the table, we have to use the [] operator. Let's add a new column named "Age" into "aa" csv file.

import pandas as pd
aa = pd.read_csv("aa.csv")
aa["Age"] = "24"
aa.head()

This code adds a column "Age" at the end of the aa csv file. So, the new table after adding a column will look like this:

    Name           Hire Date    Salary    Leaves Remaining  Age
0  John Idle        03/15/14    50000.0     10              24
1  Smith Gilliam    06/01/15    65000.0     8               24 
2  Parker Chapman   05/12/14    45000.0     10              24
3  Jones Palin      11/01/13    70000.0     3               24
4  Terry Gilliam    08/12/14    48000.0     7               24
5  Michael Palin    05/23/13    66000.0     8               24

In the above code, Age value has defined the universal value that means its value is common to all the rows. If we specify a column name that does not exist, Pandas will throw an error.

For ex:

aa["Designation"]

In the above code, Pandas will throw an error because the Designation column does not exist.

But if we assign a value to that column, Pandas will generate a new column automatically at the end of the table.

Add new columns in a DataFrame using insert()

We can also add a new column at any position in an existing DataFrame using a method name insert.

For the demonstration, first, we have to write a code to read the existing file that consists of some columns in a DataFrame.

import pandas as pd
aa = pd.read_csv("aa.csv")
aa.head()s

The above code read the existing csv file and shown the data values column in the output.

Output

    Name           Hire Date   Salary    Leaves Remaining
0  John Idle       03/15/14    50000.0    10
1  Smith Gilliam   06/01/15    65000.0    8
2  Parker Chapman  05/12/14    45000.0    10
3  Jones Palin     11/01/13    70000.0    3
4  Terry Gilliam   08/12/14    48000.0    7
5  Michael Palin   05/23/13    66000.0    8

Let's add a new column name "Department" into an existing "aa" csv file using insert method.

import pandas as pd
aa = pd.read_csv("aa.csv")
aa.insert(2, column = "Department", value = "B.Sc")
aa.head()

Output

   Name            Hire Date   Department   Salary    Leaves Remaining
0  John Idle       03/15/14     B.Sc        50000.0    10
1  Smith Gilliam   06/01/15     B.Sc        65000.0    8
2  Parker Chapman  05/12/14     B.Sc        45000.0    10
3  Jones Palin     11/01/13     B.Sc        70000.0    3
4  Terry Gilliam   08/12/14     B.Sc        48000.0    7
5  Michael Palin   05/23/13     B.Sc        66000.0    8





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf