Created
July 16, 2019 07:28
-
-
Save vikas-git/c08be90e03fadfcdb776ccc38bc263ea to your computer and use it in GitHub Desktop.
Script for import data from csv to mongo DB using Pandas dataframe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import pandas as pd | |
import pymongo | |
def import_content(filepath): | |
mng_client = pymongo.MongoClient('localhost', 27017) | |
mng_db = mng_client['route_planning'] | |
collection_name = 'lat_long' | |
db_cm = mng_db[collection_name] | |
cdir = os.path.dirname(__file__) | |
file_res = os.path.join(cdir, filepath) | |
data = pd.read_csv(file_res) | |
data = data[['node', 'latitude', 'longitude']] | |
data['id'] = list(range(1, 2583)) | |
# data = data[data.duplicated(subset=['latitude','longitude'], keep=False)] | |
# print(data) | |
data_json = json.loads(data.to_json(orient='records')) | |
# print(data_json) | |
# db_cm.remove() | |
db_cm.insert(data_json) | |
if __name__ == "__main__": | |
import_content('lat_long_collection.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment