-
Notifications
You must be signed in to change notification settings - Fork 0
/
up_to_atlas.py
30 lines (24 loc) · 894 Bytes
/
up_to_atlas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
from pymongo import MongoClient
from dotenv import (load_dotenv, find_dotenv)
# find and load environment keys
load_dotenv(find_dotenv())
user = os.getenv('uname')
passwd = os.getenv('passwd')
host = os.getenv('hname')
# set the connection key to connect to atlas
connection_string = "mongodb+srv:https://{}:{}@{}/".format(user, passwd, host)
mongo_client = MongoClient(connection_string)
# get the database and the collection
db = mongo_client['tweetsdb']
collection = db['tweet_data']
def pandas_to_atlas(df1, df2):
"""
a function to convert and upload pandas Dataframe to mongo atlas
"""
df1.reset_index(inplace=True)
df2.reset_index(inplace=True)
if collection.insert_many(df1.to_dict('records')) and collection.insert_many(df2.to_dict('records')):
print('Uploaded to atlas Successfully')
else:
print('Error whiles uploading to atlas')