Pandas Useful skill

last update 20200823

Rename a columns

df.columns=['New Col 1' ,'New Col ','New Col 3']

  

Insert DataFrame to Mongo

from pymongo import MongoClient


def connect_mongo(dbcollectionhost='xxxxx'port=27017):
    client = MongoClient(host, port)
    db = client[db][collection]
    return db
 
db.insert_many(df.to_dict('records'))
 
 
#Query Data from Mongo to pandas
df=pd.DataFrame(list(db.find({}.{'_id':False}))
 
#Select time. First need to set the field to DateTime
df=df.set_index(pd.DateTimeindex(df1['date'])
 
#only need data in trading hour
df.between_time('9:00','16:30') 
 
 
df= df[df['date']>"2021-01-01"] 
 

 

Merge 2 DataFrame

 mpf=pd.merge(df,chk,left_on=['date'],right_on=['date'])


Locate first row date

df.iloc[0]


Display all row

pandas.set_option('display.max_rows', None)

留言