I have 4 files named "TraversedEdgesZ13Set1.CSV", "RoutesZ13.CSV", "Volume.xlsx" and…
- User
I have 4 files named "TraversedEdges_Z1_3_Set1.CSV", "Routes_Z1_3.CSV", "Volume.xlsx" and "Gravity.xlsx"located at "C:\amir\my project\Python". Your task is to write a Python program using the pandas library to perform the following steps:
1-Read the "TraversedEdges_Z1_3_Set1.CSV" file and load the data into a pandas dataframe. 2-Read the "Routes_Z1_3.CSV" file and load the data into another pandas dataframe. 3-Merge the two dataframes based on the "RouteID" and "ObjectID" columns, keeping all the rows from the "TraversedEdges_Z1_3_Set1" dataframe. 4-Create a new column named "OrigDest" in the 17th column of the "TraversedEdges_Z1_3_Set1" dataframe. 5-Populate the "OrigDest" column with the values from the "Name" column of the merged dataframe. 6-Create two new columns named "Origin" and "Destination" in the 18th and 19th column of the "TraversedEdges_Z1_3_Set1" dataframe. 7-Populate the "Origin" column with the value from "Name" column of the merged dataframe. but only use the date before "-" and populate the "Destination" column with date after "-". 8-Exclude the "ObjectID" and "Name" columns from the final result. 9-Overwrite the original "TraversedEdges_Z1_3_Set1.CSV" file with the updated dataframe, including the newly added "OrigDest" column. Write a Python program that accomplishes these tasks using the pandas library.
- Assistant
import pandas as pd
Read TraversedEdges_Z1_3_Set1.CSV file
traversed_edges = pd.read_csv('C:/amir/my project/Python/TraversedEdges_Z1_3_Set1.CSV')
Read Routes_Z1_3.CSV file
routes = pd.read_csv('C:/amir/my project/Python/Routes_Z1_3.CSV')
Merge the two dataframes based on the "RouteID" and "ObjectID" columns
merged_df = pd.merge(traversed_edges, routes, on=['RouteID', 'ObjectID'], how='left')
Create a new column named "OrigDest" in the 17th column of the "TraversedEdges_Z1_3_Set1" dataframe
merged_df.insert(16, 'OrigDest', "")
Populate the "OrigDest" column with the values from the "Name" column of the merged dataframe
merged_df['OrigDest'] = merged_df['Name']
Create two new columns named "Origin" and "Destination" in the 18th and 19th column of the "TraversedEdges_Z1_3_Set1" dataframe
merged_df.insert(17, 'Origin', "") merged_df.insert(18, 'Destination', "")