LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

Python Data Science Pandas Data Frames
A Pandas DataFrame is a 2 dimensional data structure, like a 2 dimensional array, or a table with rows and columns.
How to Create A Simple Pandas DataFrame?
Example 1: Create a simple Pandas DataFrame.
Code
import pandas as pd
data = {
'Fruits': ["Mangoes", "Apples","Bananas", "Grapes", "Oranges"],
'Popularity Ranks': [1, 2, 3, 4, 5]
}
#load data into a DataFrame object:
df = pd.DataFrame(data)
print(df)
the output will be
Fruits | Ranks | 0 | Mangoes | 1 |
---|---|---|
1 | Apples | 2 |
2 | Bananas | 3 |
3 | Grapes | 4 |
4 | Oranges | 5 |
From the result above, it is clear that the DataFrame is like a table with rows and columns.
Pandas uses the loc attribute to return one or more specified row(s).
Example 2: Return row 0.
Code
import pandas as pd
data = {
'Fruits': ["Mangoes", "Apples","Bananas", "Grapes", "Oranges"],
'Popularity Ranks': [1, 2, 3, 4, 5]
}
#load data into a DataFrame object:
df = pd.DataFrame(data)
print(df.loc[0])
the output will be
Fruits Mangoes
Popularity Ranks 1
Name: 0, dtype: object
Example 3: Return row 0 and 1.
Code
import pandas as pd
data = {
'Fruits': ["Mangoes", "Apples","Bananas", "Grapes", "Oranges"],
'Popularity Ranks': [1, 2, 3, 4, 5]
}
#load data into a DataFrame object:
df = pd.DataFrame(data)
print(df.loc[[0, 1]])
the output will be
Fruits Ranks
0 Mangoes 1
1 Apples 2
Note: When using [], the result is a Pandas DataFrame.
Named Indexes: With the index argument, you can name your own indexes.
Example 4: Add a list of names to give each row a name.
Code
import pandas as pd
data = {
"Movies": [1, 2, 3],
"Duration": [120, 150, 240]
}
df = pd.DataFrame(data, index = ["shark 1", "shark 2", "shark 3"])
print(df)
the output will be
Movies Duration
shark 1 1 120
shark 2 2 150
shark 3 3 240
Locate Named Indexes: Use the named index in the loc attribute to return the specified row(s).
Example 5: Return "shark3".
Code
import pandas as pd
data = {
"Movies": [1, 2, 3],
"Duration": [120, 150, 240]
}
df = pd.DataFrame(data,
print(df.loc["shark 3"])
the output will be
Movies 3
Duration 240
Name: shark 3, dtype: int64
Load Files Into a DataFrame
If your data sets are stored in a file, Pandas can load them into a DataFrame.
Example 6: Load a comma separated file (CSV file) into a DataFrame.
Code
import pandas as pd
df = pd.read_csv('tennis.csv')
print(df)
the output will be
index = ["shark 1", "shark 2", "shark 3"])