Pandas DataFrames
A DataFrame is a table of data with labelled rows and columns, and it is the object you will use most in Pandas.
Creating a DataFrame From a Dictionary
Each key becomes a column name, and each list becomes the values in that column:
Example
import pandas as pd
df = pd.DataFrame({
"name": ["Ali", "Ayesha", "Hamza", "Sana"],
"course": ["Python", "Web", "Python", "Graphic"],
"fee": [15000, 18000, 15000, 12000]
})
print(df)name course fee 0 Ali Python 15000 1 Ayesha Web 18000 2 Hamza Python 15000 3 Sana Graphic 12000
From a List of Rows
You can also give a list of rows and name the columns:
Example
import pandas as pd
rows = [["Karachi", 20.4], ["Lahore", 13.0], ["Faisalabad", 3.6]]
cities = pd.DataFrame(rows, columns=["city", "population_m"])
print(cities)city population_m 0 Karachi 20.4 1 Lahore 13.0 2 Faisalabad 3.6
Basic Information
| Attribute | What it gives |
|---|---|
df.shape | (number of rows, number of columns) |
df.columns | The column names |
df.index | The row labels |
df.dtypes | The type of each column |
len(df) | The number of rows |
Example
import pandas as pd
df = pd.DataFrame({
"name": ["Ali", "Ayesha", "Hamza"],
"fee": [15000, 18000, 15000]
})
print(df.shape)
print(list(df.columns))
print(df.dtypes)(3, 2) ['name', 'fee'] name str fee int64 dtype: object
Text columns show the type str in Pandas 3. Older versions (Pandas 2) show object instead.
Selecting Columns
One column in square brackets gives a Series. A list of columns gives a new DataFrame:
Example
import pandas as pd
df = pd.DataFrame({
"name": ["Ali", "Ayesha", "Hamza"],
"course": ["Python", "Web", "Python"],
"fee": [15000, 18000, 15000]
})
print(df["name"])
print(df[["name", "fee"]])0 Ali
1 Ayesha
2 Hamza
Name: name, dtype: str
name fee
0 Ali 15000
1 Ayesha 18000
2 Hamza 15000Mind the double brackets: df[["name", "fee"]]. The outer brackets select, and the inner brackets are a Python list of column names.
Selecting Rows
Use loc with row labels or iloc with positions:
Example
import pandas as pd
df = pd.DataFrame({
"name": ["Ali", "Ayesha", "Hamza"],
"fee": [15000, 18000, 15000]
})
print(df.loc[1])
print(df.iloc[[0, 2]])name Ayesha
fee 18000
Name: 1, dtype: object
name fee
0 Ali 15000
2 Hamza 15000Setting a Column as the Index
Often one column is a natural label for each row, like a roll number or a name:
Example
import pandas as pd
df = pd.DataFrame({
"name": ["Ali", "Ayesha", "Hamza"],
"fee": [15000, 18000, 15000]
})
df = df.set_index("name")
print(df)
print(df.loc["Ayesha", "fee"])fee name Ali 15000 Ayesha 18000 Hamza 15000 18000
df.loc[row, column] gets a single value. Use df.reset_index() to turn the index back into a normal column.
Exercise
Exercise
What does df[["a", "b"]] return?
