Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Adding new column to existing DataFrame in Pandas by assigning a list

# 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)

# declare a new list and add the values into the list
match_lost = [2, 1, 3, 4]

# assign the list to the new DataFrame Column
df["lost"] = match_lost

# Print the new DataFrame
print(df)
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

Adding new column to existing DataFrame in Pandas

# Import pandas package
import pandas as pd
  
# Define a dictionary containing Students data
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
        'Height': [5.1, 6.2, 5.1, 5.2],
        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
  
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
  
# Declare a list that is to be converted into a column
address = ['Delhi', 'Bangalore', 'Chennai', 'Patna']
  
# Using 'Address' as the column name
# and equating it to the list
df['Address'] = address
  
# Observe the result
print(df)
Comment

Adding new column to existing DataFrame in Pandas

# 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)


# insert the new column at the specific position
df.insert(3, "lost", [2, 1, 3, 4], True)

# Print the new DataFrame
print(df)
Comment

PREVIOUS NEXT
Code Example
Python :: how to restart program in python 
Python :: Math Module log() Function in python 
Python :: python web parser 
Python :: discord.py get user input 
Python :: pyqt5 button example 
Python :: spacy ner 
Python :: how do you see if a data type is an integer python 
Python :: python check if string has space 
Python :: how to multiply two arrays in python 
Python :: case statement in querset django 
Python :: catch error python 
Python :: python3 change file permissions 
Python :: port 5432 failed: Connection timed out (0x0000274C/10060) Is the server running on that host and accepting TCP/IP connections? 
Python :: python select columns with no na 
Python :: django on_delete options 
Python :: button size tkinter 
Python :: frequency spectrum signal python 
Python :: python string match ignore case 
Python :: python xml parser 
Python :: pandas add column with constant value 
Python :: reverse geocode python 
Python :: how to find empty rows of a dataset in python 
Python :: python dictonary of dictonary 
Python :: keras.layers.MaxPool2D 
Python :: fastest clicker python 
Python :: python numpy array replace nan with string 
Python :: tf dropout 
Python :: Python program to check Co-Prime Number 
Python :: check if camera is being used python 
Python :: random python 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =