[go: up one dir, main page]

0% found this document useful (0 votes)
2 views1 page

Pandas Dataframe Assignment

The document outlines various methods for creating a DataFrame in pandas, including using lists, dictionaries, NumPy arrays, and lists of dictionaries. It also explains how to add a new column to an existing DataFrame through direct assignment or the insert() method. Examples are provided for each method to illustrate their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Pandas Dataframe Assignment

The document outlines various methods for creating a DataFrame in pandas, including using lists, dictionaries, NumPy arrays, and lists of dictionaries. It also explains how to add a new column to an existing DataFrame through direct assignment or the insert() method. Examples are provided for each method to illustrate their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Pandas DataFrame Assignment

1. Different Ways to Create a DataFrame


A DataFrame in pandas can be created in multiple ways. Below are some common methods with
examples:

- Using Lists
Example:
df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])

- Using Dictionary
Example:
data = {'Name': ['John', 'Anna'], 'Age': [25, 30]}
df = pd.DataFrame(data)

- Using NumPy Array


Example:
import numpy as np
data = np.array([[1, 2, 3], [4, 5, 6]])
df = pd.DataFrame(data, columns=['X', 'Y', 'Z'])

- Using List of Dictionaries


Example:
data = [{'A': 1, 'B': 2}, {'A': 3, 'B': 4}]
df = pd.DataFrame(data)

2. Adding a Column to an Existing DataFrame


A new column can be added to an existing DataFrame using assignment or the insert() method.

- Using Direct Assignment:


df['New_Column'] = [value1, value2, ...]

- Using insert() Method:


df.insert(1, 'Inserted_Column', [data1, data2])

You might also like