[Solved] Column Order and New Entry using Pandas Python

Column Order and New Entry: Digvijay is going to manage the event data. He is having the data as dataframe, and his manager is requesting him to change the order as cost column and become the 2nd column of the dataframe, and then add new entry in the dataframe. 

So help Digvijay to change the order of the cost column as the 2nd column of the dataframe and add new entry in the dataframe.

The following is the 2D format for the dataframe:   
customer_name, address, date, eventType, cost, noOfPeople
Here eventType is described as marriage, get together, and mehndi.  

Column Order and New Entry using Pandas

Filled the data in the dataframe as the CSV file, in above format.

Input Format:
For sample data frame, see the attached CSV file.

Output Format:
The output is the new dataframe after changing order the cost column and added the new entry.

Note: All bold corresponds to input, rest corresponds to output.

Sample Input and Output: 
Enter the details:
Vicky, 2300, Delhi, 12/02/1993, Mehndi, 200

cust_name, cost, addr, date, event_type, no_of_people 

Vikram, 40000, Hebbal, 08/20/2018, marriage, 400

Sachin, 75000, Indore, 04/14/2019, mehndi, 700

Amit, 20000, Bhopal, 08/20/2018, marriage, 600

Ankita, 85000, Delhi, 08/24/2019, mehndi, 850

Vicky, 2300, Delhi, 12/02/1993, Mehndi, 200

Description of the Sample Input/Output 1:
Sample Dataframe:  

cust_name, addr, date, event_type, cost, no_of_people
Vikram, Hebbal, 08/20/2018, marriage, 40000, 400
Sachin, Indore, 04/14/2019, mehndi, 75000, 700
Amit, Bhopal, 08/20/2018, marriage, 20000, 600
Ankita, Delhi, 08/24/2019, mehndi, 85000, 850

Dataframe after changing the order of the cost column: 

cust_name, cost, addr, date, event_type, no_of_people 

Vikram, 40000, Hebbal, 08/20/2018, marriage, 400

Sachin, 75000, Indore, 04/14/2019, mehndi, 700

Amit, 20000, Bhopal, 08/20/2018, marriage, 600

Ankita, 85000, Delhi, 08/24/2019, mehndi, 850 

Dataframe after adding the new entry: 

cust_name, cost, addr, date, event_type, no_of_people 

Vikram, 40000, Hebbal, 08/20/2018, marriage, 400

Sachin, 75000, Indore, 04/14/2019, mehndi, 700

Amit, 20000, Bhopal, 08/20/2018, marriage, 600

Ankita, 85000, Delhi, 08/24/2019, mehndi, 850

Vicky, 2300, Delhi, 12/02/1993, Mehndi, 200

Solution:

dataFrame1.csv

cust_name,addr,date,event_type,cost,no_of_people
Vikram,Hebbal,08/20/2018,marriage,40000,400
Sachin,Indore,04/14/2019,mehndi,75000,700
Amit,Bhopal,08/20/2018,marriage,20000,600
Ankita,Delhi,08/24/2019,mehndi,85000,850
import pandas as pd

df = pd.read_csv("dataFrame1.csv")

df = df.reindex(['cust_name', 'cost', 'addr', 'date', 'event_type', 'no_of_people'], axis=1)

print("Enter the details:")

df.loc[len(df)] = input().split(",")

print(df)

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *