Answers for "add a list to a pandas dataframe as a column"

1

pandas add column from list

# creating a list for new column
places = ['Nellore', 'Mumbai', 'Andhra']

# we are using 'Places' as column name
# adding the list to the dataframe as column
dataframe['Places'] = places
Posted by: Guest on September-17-2021
1

how to add new column to dataframe

# 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 
df
Posted by: Guest on September-17-2020
0

how to add a list to dataframe in python

to_append = [5, 6]
a_series = pd.Series(to_append, index = df.columns)
df = df.append(a_series, ignore_index=True)
Posted by: Guest on September-01-2021

Code answers related to "add a list to a pandas dataframe as a column"

Python Answers by Framework

Browse Popular Code Answers by Language