Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

add a column with initial value to an existing dataframe

new_df=df.assign(Profit=6)
Comment

Adding new column to existing DataFrame in Pandas using assign method

# import pandas library
import pandas as pd

# create pandas DataFrame
df = pd.DataFrame({'team': ['India', 'South Africa', 'New Zealand', 'England'],
                   'points': [10, 8, 3, 5],
                   'runrate': [0.5, 1.4, 2, -0.6],
                   'wins': [5, 4, 2, 2]})

# print the DataFrame
print(df)

# append multiple columns to Pandas DataFrame
df2 = df.assign(lost=[2, 1, 3, 4], matches_remaining=[2, 3, 1, 1])

# Print the new DataFrame
print(df2)
Comment

PREVIOUS NEXT
Code Example
Python :: switch case dictionary python 
Python :: python3 call parent constructor 
Python :: merge two sorted lists into one sorted list 
Python :: Combine integer in list 
Python :: if number py 
Python :: find string in list and return index python 
Python :: python implementation of Min Heap 
Python :: get request in django 
Python :: check if a file exists in python 
Python :: arrays in python 
Python :: how to delete record in django 
Python :: django on delete set default 
Python :: numpy make 2d array 1d 
Python :: sort lexo python 
Python :: recursive binary search python 
Python :: python program to find sum of natural numbers using recursion 
Python :: function with args* example 
Python :: how to check if given primary key exists in django model 
Python :: dict_keys to list 
Python :: df.loc a list of index 
Python :: pos taggging in nltk 
Python :: how to drop columns from pandas dataframe 
Python :: python read from stdin pipe 
Python :: python plot speichern 
Python :: python how to print variable value 
Python :: How to show variable in Jupyter 
Python :: firebase functions python 
Python :: mathplolib avec date 
Python :: python select columns names from dataframe 
Python :: fetch json array from mysql django 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =