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