In Pandas, you can add a column to an existing DataFrame using either bracket notation or the assign method. Here's an example using bracket notation:
python
Copy code
import pandas as pd
Create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
Add a new column 'C' with a list of values
df['C'] = [7, 8, 9]
Print the updated DataFrame
print(df)
In this example, we first create a DataFrame with two columns 'A' and 'B'. We then use bracket notation to add a new column 'C' with a list of values. The length of the list must match the number of rows in the DataFrame. Finally, we print the updated DataFrame to confirm that the new column has been added.
Alternatively, you can use the assign method to add a new column as a copy of an existing column with a modified value, like this:
python
Copy code
Add a new column 'D' as a copy of column 'A' multiplied by 2
df = df.assign(D=df['A'] * 2)
Print the updated DataFrame
print(df)
In this example, we use the assign method to create a new column 'D' as a copy of column 'A' multiplied by 2. The assign method returns a new DataFrame with the new column added, so we assign the result back to df. Finally, we print the updated DataFrame to confirm that the new column has been added.
code snippet
import pandas as pd
import numpy as np
d = {'one':pd.Series([1,2,3],index=['a','b','c']),
'two':pd.Series([1,2,3,4],index = ['a','b','c','d'])}
df = pd.DataFrame(d)
print(df)
print('adittion of a new column by adition of new Series:')
df['three'] = pd.Series([10,20,30],index=['a','b','c'])
print(df)
@ParagDhawan
=============================================================================
Link for Tutorial Series
Jupyter Notebook Tutorial Series:-
• How To Open Jupyter Notebook in Windows
Python Tutorial Series:-
• Introduction to Python | Python Appli...
Python Assignments and Objective Questions:-
• Objective Questions Python - 1
Tech. Videos By Parag Dhawan;-
• Template Matching Using OpenCV (Pytho...
Object-Oriented Programming in Python:-
• How to Create Class and Object in Python
File Handling in Python:-
• How to Create a file in Python | Pyth...
Exception Handling in Python:-
• Exception Handling in Python
NumPy Tutorial Series:-
• NumPy
=============================================================================
Feel free to connect and ask your queries:-
Linkedin:- / parag-dhawan
Youtube:- / paragdhawan
Facebook Page:- http://fb.me/dhawanparag
Instagram: - / paragdhawan
Twitter:- / dhawan_parag
GitHub:- https://github.com/paragdhawan/
=============================================================================
Show your support by Subscribing to the channel:-
https://www.youtube.com/c/ParagDhawan...
=============================================================================
#ParagDhawan
#Pandas
#DataScience
#DataAnalysis
#PandasTutorial
#PandasCourse
#Python3
#Python
#PythonProgramming
============================================================
How to Record Your Screen and make a tutorial video or demo video: -
• How to Record Screen and Edit Video U...
============================================================