DataFrame

2.3. DataFrame#

In the example below, we start by creating a dictionary data that contains information about names, ages, and cities. We then use this dictionary to create a dataframe df using pd.DataFrame( ).

data2 = {'Name': ['John', 'Alice', 'Bob'],
    'Age': [25, 28, 22],
    'City': ['London', 'Paris', 'Berlin']}

df = pd.DataFrame(data2)

print("Original DataFrame:")
df      
Original DataFrame:
Name Age City
0 John 25 London
1 Alice 28 Paris
2 Bob 22 Berlin

You can then use the loc[ ] and loc[ ] functions to locate specific data from your dataframe. For example:

print("\nUsing iloc[]:")
print(df.iloc[0])                
print(df.iloc[1:3])               
print(df.iloc[0, 1])             

print("\nUsing loc[]:")
print(df.loc[0])                 
print(df.loc[1:2])                
print(df.loc[0, 'Age'])    
Using iloc[]:
Name      John
Age         25
City    London
Name: 0, dtype: object
    Name  Age    City
1  Alice   28   Paris
2    Bob   22  Berlin
25

Using loc[]:
Name      John
Age         25
City    London
Name: 0, dtype: object
    Name  Age    City
1  Alice   28   Paris
2    Bob   22  Berlin
25

We then add and remove some data from the created dataframe. You can remove rows and columns from a datafram by using the Yourdataframename.drop() function.

df.loc[0, 'City'] = 'New York'
df['Salary'] = [5000, 6000, 4500]

df = df.drop(1)                   
df = df.drop('Age', axis=1)      

print("\nUpdated DataFrame:")
df
Updated DataFrame:
Name City Salary
0 John New York 5000
2 Bob Berlin 4500