Program Dataframe
Program Dataframe
Program Dataframe
• Print the batsman name along with runs scored in Test and T20 using
column names and dot notation.
import pandas as pd
data = pd.DataFrame(player_data)
# ----------------------
values = {'Name':['Rishabh Pant','ShreyasIyer'],'Test':
[1500,1459],'ODI':[1980,1342],'T20':[2300,1988]}
data = data.append(pd.DataFrame(values), ignore_index=True)
print(data)
# -----
print("Data after deleting index 1 and 4")
data = data.drop([1,4])
print(data)
• Delete a column Test, add one more column at last (next to T20
column), make total of ODI and T20 runs in that column.
import pandas as pd
# Creating the Data
player_data = {"Name":["ViratKohli","AjinkyaRahane","Rohit
Sharma","ShikharDhawan","Hardik Pandya"],
"Test" : [3543,2578,2280,2158,1879],
"ODI" : [2245,2165,2080,1957,1856],
"T20" : [1925,1853,1522,1020,980]
}
data = pd.DataFrame(player_data)
# The following line is used to start the index from 1
data.index = data.index + 1
# ----------------------
data = data.drop('Test', axis = 1)
print(data)
# ---------------
total = data['ODI'] + data["T20"]
data['Total'] = total
print(data)
• Delete columns using T20 and total using columns parameter in
drop() function.
import pandas as pd
data = pd.DataFrame(player_data)
# The following line is used to start the index from 1
data.index = data.index + 1
# ---------------
total = data['ODI'] + data["T20"]
data['Total'] = total
data = data.drop(['T20','Total'], axis = 1)
print(data)
• Rename column T20 with “T20I Runs”.
import pandas as pd
data = pd.DataFrame(player_data)
# The following line is used to start the index from 1
data.index = data.index + 1
# ----------------------
data.rename(columns={'T20':'T20I runs'}, inplace = True)
print(data)
• Rename all the columns of dataframe with your choice of column
names.
import pandas as pd
data = pd.DataFrame(player_data)
# The following line is used to start the index from 1
data.index = data.index + 1
# ----------------------
data.rename(columns={'T20':'T20I
runs','Name':'PlayerName','Test':'TestRuns','ODI':'ODI Runs'},
inplace = True)
print(data)
• Rename the index with prefix IND and number like 001 and So on.
import pandas as pd
# Creating the Data
player_data = {"Name":["ViratKohli","AjinkyaRahane","Rohit
Sharma","ShikharDhawan","Hardik Pandya"],
"Test" : [3543,2578,2280,2158,1879],
"ODI" : [2245,2165,2080,1957,1856],
"T20" : [1925,1853,1522,1020,980]
}
data = pd.DataFrame(player_data)
# The following line is used to start the index from 1
data.index = data.index + 1
# ----------------------
index = pd.Index(["001","002","003","004","005"])
data = pd.DataFrame(player_data,index)
data = data.rename_axis('IND')
print(data)
3. Display the first two rows and last two rows.
import pandas as pd
data = pd.DataFrame(player_data)
# The following line is used to start the index from 1
data.index = data.index + 1
# ----------------------
print(data.head(2))
print(data.tail(2))
• Count the total number of rows and columns of the dataframe.
import pandas as pd
data = pd.DataFrame(player_data)
# The following line is used to start the index from 1
data.index = data.index + 1
# ----------------------
total_rows = len(data.axes[0])
total_col = len(data.axes[1])
data = pd.DataFrame(player_data)
# The following line is used to start the index from 1
data.index = data.index + 1
# ----------------------
print(data.to_csv(header=None,index=False))