0% found this document useful (0 votes)
3 views2 pages

Dataframe Syntax

The document provides a guide on using the Pandas library in Python, covering importing the library, creating DataFrames from various data sources, and accessing and modifying data within those DataFrames. It includes examples of creating DataFrames from dictionaries and lists, selecting columns and rows, adding new columns, and performing basic operations like viewing data and generating statistics. Overall, it serves as a foundational overview for manipulating data using Pandas.

Uploaded by

trt.rmms.kpwd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Dataframe Syntax

The document provides a guide on using the Pandas library in Python, covering importing the library, creating DataFrames from various data sources, and accessing and modifying data within those DataFrames. It includes examples of creating DataFrames from dictionaries and lists, selecting columns and rows, adding new columns, and performing basic operations like viewing data and generating statistics. Overall, it serves as a foundational overview for manipulating data using Pandas.

Uploaded by

trt.rmms.kpwd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Importing Pandas:

The first step is to import the pandas library, typically aliased as pd.

Python

import pandas as pd

2. Creating a DataFrame:

DataFrames can be created from various data sources, including dictionaries, lists, NumPy
arrays, or other DataFrames.

 From a dictionary: This is a common method where keys become column names and
values are lists of data for each column.

Python

data = {'Name': ['Alice', 'Bob', 'Charlie'],


'Age': [25, 30, 22],
'City': ['New York', 'London', 'Paris']}
df = [Link](data)

 From a list of lists (or NumPy array): You can also create a DataFrame from nested
lists, specifying column names separately.

Python

data = [['Alice', 25, 'New York'],


['Bob', 30, 'London'],
['Charlie', 22, 'Paris']]
df = [Link](data, columns=['Name', 'Age', 'City'])

3. Accessing Data:

 Selecting columns: Access columns using square bracket notation with the column
name.

Python

names = df['Name'] # Returns a Series


ages_cities = df[['Age', 'City']] # Returns a DataFrame

 Selecting rows: Use .loc for label-based indexing and .iloc for integer-based indexing.

Python

first_row = [Link][0] # Accesses by label (index)


first_row_iloc = [Link][0] # Accesses by integer position

4. Modifying Data:
 Adding a new column: Assign a new Series or list to a new column name.

Python

df['Country'] = ['USA', 'UK', 'France']

 Modifying existing values: Select the specific cell or cells and assign new values.

Python

[Link][0, 'Age'] = 26

5. Basic Operations:

DataFrames offer numerous built-in methods for data analysis and manipulation, such as:

 [Link](): View the first few rows.

 [Link](): Get a summary of the DataFrame.

 [Link](): Generate descriptive statistics.

 [Link]: Get the dimensions (rows, columns).

You might also like