How to export pandas data to elasticsearch?

前端 未结 4 2138
独厮守ぢ
独厮守ぢ 2021-02-20 00:26

It is possible to export a pandas dataframe data to elasticsearch using elasticsearch-py. For example, here is some code:

https://www.analyticsvidhya.com/bl

4条回答
  •  走了就别回头了
    2021-02-20 00:41

    may you can use

    pip install es_pandas
    pip install progressbar2
    

    This package should work on Python3(>=3.4) and ElasticSearch should be version 5.x, 6.x or 7.x.

    import time
    import pandas as pd
    from es_pandas import es_pandas
    
    
    # Information of es cluseter
    es_host = 'localhost:9200'
    index = 'demo'
    
    # crete es_pandas instance
    ep = es_pandas(es_host)
    
    # Example data frame
    df = pd.DataFrame({'Alpha': [chr(i) for i in range(97, 128)], 
                        'Num': [x for x in range(31)], 
                        'Date': pd.date_range(start='2019/01/01', end='2019/01/31')})
    
    # init template if you want
    doc_type = 'demo'
    ep.init_es_tmpl(df, doc_type)
    
    # Example of write data to es, use the template you create
    ep.to_es(df, index, doc_type=doc_type)
    # set use_index=True if you want to use DataFrame index as records' _id
    ep.to_es(df, index, doc_type=doc_type, use_index=True)
    

    here is the document https://pypi.org/project/es-pandas/
    if 'es_pandas' cann't solve you problem,you could see other solution : https://towardsdatascience.com/exporting-pandas-data-to-elasticsearch-724aa4dd8f62

提交回复
热议问题