# Pandas - dataframe
import pandas as pd
data = {
"Countries": ["India","China","Spain","Italy","USA", "UK", "France","Australia"],
"Capital": ["New Delhi", "Beijing", "Madrid", "Rome", "Washington DC", "London", "Paris", "Canberra"],
"Population (Million)":[1380,1439,46,60,331,67,65,25]
}
#load data into a DataFrame object:
df = pd.DataFrame(data)
print("Unsorted data")
print(df)
# Sorting data by column
sorted_df = df.sort_values(by='Population (Million)')
print("Data sorted by population")
print(sorted_df)
# Printing selected columns
df_SelectedCols = pd.DataFrame(data, columns=["Countries","Population (Million)"])
print("Printing selected columns")
print(df_SelectedCols)
"""
Sample output
>python Pandas1.py
Unsorted data
Countries Capital Population (Million)
0 India New Delhi 1380
1 China Beijing 1439
2 Spain Madrid 46
3 Italy Rome 60
4 USA Washington DC 331
5 UK London 67
6 France Paris 65
7 Australia Canberra 25
Data sorted by population
Countries Capital Population (Million)
7 Australia Canberra 25
2 Spain Madrid 46
3 Italy Rome 60
6 France Paris 65
5 UK London 67
4 USA Washington DC 331
0 India New Delhi 1380
1 China Beijing 1439
Printing selected columns
Countries Population (Million)
0 India 1380
1 China 1439
2 Spain 46
3 Italy 60
4 USA 331
5 UK 67
6 France 65
7 Australia 25
"""
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu