import pandas as pd
# Data defined in a dictionary
data = {
"Name": ["Alice", "Bob", "Charlie"],
"Age": [25, 30, 22],
"City": ["New York", "San Francisco", "Los Angeles"]
}
# Create DataFrame
df = [Link](data)
print(df)
changing index values
Name Age City
0 Alice 25 New York
1 Bob 30 San Francisco
2 Charlie 22 Los Angeles
>>>df.set_index('Name', inplace=True)
>>>df
Age City
Name
Alice 25 New York
Bob 30 San Francisco
Charlie 22 Los Angeles
Replacing index with new values
>>>[Link](index={0: 'A', 1: 'B', 2: 'C'}, inplace=True)
>>>df
Name Age City
A Alice 25 New York
B Bob 30 San Francisco
C Charlie 22 Los Angeles
Reseting index
>>>df.reset_index(inplace=True)
>>>df
index Name Age City
0 A Alice 25 New York
1 B Bob 30 San Francisco
2 C Charlie 22 Los Angeles
# rename column 'Name' to 'First_Name'
[Link](columns= {'Name': 'First_Name'}, inplace=True)
# rename columns 'Age' and 'City'
[Link](mapper= {'Age': 'Number', 'City':'Address'}, axis=1, inplace=True)
updating vaule
>>>[Link][1,"Age"]=10
Adding a brand new row with label 'row3' and setting the 'Name'
>>>[Link]['row3', 'Name'] = 'Charlie'
# add a new row
>>>[Link][len([Link])] = ['D','Amy', 52, 'BIT', 'Goa']
Why use .at instead of .loc ?
Speed: .at is significantly faster than .loc for accessing or setting a single scalar
value.
Safety: Unlike some other methods, .at modifies the original DataFrame in place and
avoids "SettingWithCopy" warnings.
Limitation: You can only use .at for a single value. You cannot use it for slicing
(e.g., [Link][0:2, 'Col'] will fail). Use .loc for multiple values.
Accessing ros from df
>>>[Link][1]
index B
Name Bob
Age 30
City San Francisco
Name: 1, dtype: object
Only specific rows
>>>[Link][[0,3]]
Slicing of rows
>>>[Link][1:3]
By using index value
>>>[Link][1]
index B
Name Bob
Age 30
City San Francisco
Name: 1, dtype: object
>>>[Link][0:1]
index Name Age City
0 A Alice 25 New York
>>>[Link][0:2]
index Name Age City
0 A Alice 25 New York
1 B Bob 30 San Francisco
Accessing column values
>>>df['Name']
>>>df[['Name','City']]
>>>df[['Name','City','Age']]
>>>[Link][1,'Name']
'Bob'
>>>[Link][1,'Name':'Age']
Name Bob
Age 30
Name: 1, dtype: object
>>>[Link][2,'Name':'Age']
Name Charlie
Age 22
Name: 2, dtype: object
# slicing columns from 'Name' to 'Age'
>>>[Link][:, 'Name':'Age']
Name Age
0 Alice 25
2 Charlie 22
4 Amy 52
# slicing columns from row index and column 'Name' to 'Age'
>>>[Link][0:2, 'Name':'Age']
slice_columns
>>>[Link][:, 0:2]
index Name
0 A Alice
2 C Charlie
4 D Amy
>>>[Link][1:3, ['Name', 'Age']]
>>>[Link][1:4, [0, 2]]
Adding a new column to df
>>>address = ['New York', 'London', 'Sydney']
>>>df['Address'] = address
>>>df
index Name Age City Address
0 A Alice 25 New York New York
1 B Bob 30 San Francisco London
2 C Charlie 22 Los Angeles Sydney
# add a new row
>>>[Link][len([Link])] = ['D','Amy', 52, 'BIT', 'Goa']
>>>df
index Name Age City Address
0 A Alice 25 New York New York
1 B Bob 30 San Francisco London
2 C Charlie 22 Los Angeles Sydney
3 D Amy 52 BIT Goa
Checking conditions
>>>[Link][df['Age'] > 30]
# delete row with index 3
>>>[Link](3, axis=0, inplace=True)
>>>df
index Name Age City Address
0 A Alice 25 New York New York
1 B Bob 30 San Francisco London
2 C Charlie 22 Los Angeles Sydney
# add a new row
>>>[Link][len([Link])] = ['D','Amy', 52, 'BIT', 'Goa']
# delete row with index 3
>>>[Link](3)
index Name Age City Address
0 A Alice 25 New York New York
1 B Bob 30 San Francisco London
2 C Charlie 22 Los Angeles Sydney
Delete a column from df
>>>[Link]('Age', axis=1, inplace=False)
index Name City Address
0 A Alice New York New York
1 B Bob San Francisco London
2 C Charlie Los Angeles Sydney
3 D Amy BIT Goa
# delete rows with index 1 and 3
>>>[Link]([1, 3], axis=0, inplace=True)
>>>df
index Name Age City Address
0 A Alice 25 New York New York
2 C Charlie 22 Los Angeles Sydney
# delete height and profession columns
[Link](['Age', 'Address'], axis=1, inplace=True)
checking condition
>>>[Link]('Age > 25')
>>>[Link]('Age > 25 and Name == "Bob"')
Checking value
>>>df[df['Name'].isin(['Amy'])]
>>>df[df['Age'].isin([22])]
>>>df.sort_values('Age', inplace=True)
Sort more than one column
>>>df.set_index(['Age','Address'], inplace=True)
>>>df[“age”].min()
>>>df[‘Age’].max()
>>> df[‘Age’].sum()
>>>df['Age'].aggregate('min')
>>>[Link]('Name')['Age'].agg(['sum', 'mean', 'max', 'min'])
When two or more column have same values
>>>[Link](['Gender', 'Grade']).aggregate(agg_functions)
Filtering data
>>>[Link](items=['Name', 'City'])
# replace missing values with 0
>>>[Link](value=0, inplace=True)
# remove rows with missing values
>>>[Link](inplace=True)
The pivot() function in Pandas reshapes data based on column values. It
takes simple column-wise data as input, and groups the entries into a two-
dimensional table.
Pivot Operation in Pandas
Let's look at an example.
import pandas as pd
# create a dataframe
data = {'Date': ['2023-01-01', '2023-01-01', '2023-01-02', '2023-01-02'],
'City': ['New York', 'Los Angeles', 'New York', 'Los Angeles'],
'Temperature': [32, 75, 30, 77]}
df = [Link](data)
print("Original DataFrame\n", df)
print()
# pivot the dataframe
pivot_df = [Link](index='Date', columns='City', values='Temperature')
print("Reshaped DataFrame\n", pivot_df)
Run Code
Output
Original DataFrame
Date City Temperature
0 2023-01-01 New York 32
1 2023-01-01 Los Angeles 75
2 2023-01-02 New York 30
3 2023-01-02 Los Angeles 77
Reshaped DataFrame
City Los Angeles New York
Date
2023-01-01 75 32
2023-01-02 77 30
To read data from csv file
Import pandas as pd
df = pd.read_csv('[Link]', header = 0)
print(df)
eg2 df = pd.read_csv('./csv_files/[Link]', header = 0)
df = pd.read_csv(
filepath_or_buffer,
sep=',',
header=0,
names=['col1', 'col2', 'col3'],
index_col='col1',
usecols=['col1', 'col3'],
skiprows=[1, 3],
nrows=100,
skipinitialspace=True
)
import pandas as pd
# read csv file with some arguments
df = pd.read_csv('[Link]', header = None, names = ['col1', 'col2', 'col3'],
skiprows = 2)
print(df)
import pandas as pd
# read csv file with some arguments
df = pd.read_csv('[Link]', header = None, names = ['col1', 'col2', 'col3'],
skiprows = 2)
print(df)
Write to CSV Files
We used read_csv() to read data from a CSV file into a DataFrame.
Pandas also provides the to_csv() function to write data from a DataFrame
into a CSV file.
Let's see an example.
import pandas as pd
# create a dictionary
data = {'Name': ['John', 'Alice', 'Bob'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Paris']}
# create a dataframe from the dictionary
df = [Link](data)
# write dataframe to csv file
df.to_csv('[Link]', index=False)
Output
Name,Age,City
John,25,New York
Alice,30,London
Bob,35,Paris
Here, the above code writes the DataFrame df to the [Link] file.
The index=False parameter is used to exclude the index labels from the CSV
file.
Write to CSV Files
We used read_csv() to read data from a CSV file into a DataFrame.
Pandas also provides the to_csv() function to write data from a DataFrame
into a CSV file.
Let's see an example.
import pandas as pd
# create a dictionary
data = {'Name': ['John', 'Alice', 'Bob'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Paris']}
# create a dataframe from the dictionary
df = [Link](data)
# write dataframe to csv file
df.to_csv('[Link]', index=False)
Output
Name,Age,City
John,25,New York
Alice,30,London
Bob,35,Paris
Here, the above code writes the DataFrame df to the [Link] file.
The index=False parameter is used to exclude the index labels from the CSV
file.
Write to CSV Files
We used read_csv() to read data from a CSV file into a DataFrame.
Pandas also provides the to_csv() function to write data from a DataFrame
into a CSV file.
Let's see an example.
import pandas as pd
# create a dictionary
data = {'Name': ['John', 'Alice', 'Bob'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Paris']}
# create a dataframe from the dictionary
df = [Link](data)
# write dataframe to csv file
df.to_csv('[Link]', index=False)
Output
Name,Age,City
John,25,New York
Alice,30,London
Bob,35,Paris
Here, the above code writes the DataFrame df to the [Link] file.
The index=False parameter is used to exclude the index labels from the CSV
file.
Example: to_csv() With Arguments
import pandas as pd
# create dataframe
data = {'Name': ['Tom', 'Nick', 'John', 'Tom'],
'Age': [20, 21, 19, 18],
'City': ['New York', 'London', 'Paris', 'Berlin']}
df = [Link](data)
# write to csv file
df.to_csv('[Link]', sep = ';', index = False, header = True)
Output
Name;Age;City
Tom;20;New York
Nick;21;London
John;19;Paris
Tom;18;Berlin