Add Column to pandas DataFrame in Python (2 Examples) | Append List as Variable | assign() Function

Опубликовано: 04 Октябрь 2024
на канале: Statistics Globe
200
11

How to append a new column to a pandas DataFrame in the Python programming language. More details: https://statisticsglobe.com/add-colum...
Python code of this video:

import pandas as pd # Load pandas library

data = pd.DataFrame({"x1":range(15, 20), # Create pandas DataFrame
"x2":["a", "b", "c", "d", "e"],
"x3":range(5, 0, - 1)})
print(data) # Print pandas DataFrame


new_col = ["new", "so_new", "very_new", "the_newest", "neeeew"] # Create list
print(new_col) # Print list
['new', 'so_new', 'very_new', 'the_newest', 'neeeew']

data_new1 = data.assign(new_col = new_col) # Add new column
print(data_new1) # Print new DataFrame

data_new2 = data.copy() # Create copy of original DataFrame
data_new2["new_col"] = new_col # Add new column
print(data_new2) # Print new DataFrame

Follow me on Social Media:
Facebook – Statistics Globe Page:   / statisticsglobecom  
Facebook – R Programming Group for Discussions & Questions:   / statisticsglobe  
Facebook – Python Programming Group for Discussions & Questions:   / statisticsglobepython  
LinkedIn – Statistics Globe Page:   / statisticsglobe  
LinkedIn – R Programming Group for Discussions & Questions:   / 12555223  
LinkedIn – Python Programming Group for Discussions & Questions:   / 12673534  
Twitter:   / joachimschork  

Music by bensound.com