Use of the Pythonnet module to convert a DataTable into a dataframe

Source: Internet
Author: User
Tags mongoclient python script

1. Selection of modules

There are two ways in which Python interacts with C #, one using the IronPython module and the other using the Pythonnet module. I recommend using pythonnet, because IronPython does not support some of the modules in C #, such as more famous pandas,numpy.

2. Using Pythonnet

Using PIP to download the Pythonnet module, there will be clr.pyd, Python under the installation path. Runtime.dll two files, PYD files are used in Python scripts, while Python. Runtime is a dynamic library that is called in a C # program. (Note: The downloaded pythonnet module, C # generates the same number of system bits and the number of IDE bits used, such as: x86, or x64)

3. Code

1 ImportSYS2 ImportOS3Sys.path.insert (0,os.path.abspath ('./'))4 5 ImportCLR6Clr. AddReference ('System.Data')7 8  fromSystemImportData9  fromSystem.DataImportDataSetTen  fromSystem.DataImportDataTable One  fromSystem.DataImportDataColumn A  fromSystem.DataImportDataRow -  - ImportPandas as PD the ImportNumPy as NP -  fromPymongoImportmongoclient - ImportTypes - ImportBson +  - deftabletodataframe (DT): +     " "Converting a DataTable type to a dataframe type" " AColtempcount =0 atDic={} -      while(Coltempcount <dt. Columns.count): -Li = [] -Rowtempcount =0 -ColName =dt. Columns[coltempcount]. ColumnName -          while(Rowtempcount <dt. Rows.Count): inresult =dt. Rows[rowtempcount][coltempcount] - li.append (Result) toRowtempcount = Rowtempcount + 1 +          -Coltempcount = Coltempcount + 1 the Dic.setdefault (Colname,li) *  $DF =PD. DataFrame (DIC)Panax Notoginseng     return(DF) -  the defdataframetodic (DF): +     " "convert dataframe data type to dictionary type" " ADIC = Df.to_dict ('List') the     returnDiC +  - defCollenctionmongodb (): $Datatablelist = [] $Client = Mongoclient ('localhost', 27017) -db = client['Database name'] -c = db[' Tablename'] the Datatablelist.append (c) -DS =DataSet (object)Wuyi      forDBTableinchdatatablelist: theDT =DataTable (object) -DbDR =Dbtable.find_one () Wu          forDBTableinchlist (Dbdr.keys ()): -DC =DataColumn (object) Aboutdc. ColumnName =a $ dt. Columns.Add (DC) -  -          forIteminchdbtable.find (): -Li = [] A              forBinchlist (Item.values ()): +                 ifTypes. Inttype = =type (b): the Li.append (b) -                 elifTypes. StringType = =type (b): $Li.append (B.encode ("Utf-8")) the                 elifisinstance (b,bson.object.object): the                     Pass the                 Else: the Li.append (b) -  in dt. Rows.Add (LI) the ds. Tables.add (DT) the     returnDS About  the if __name__=='__main__': the ds. Collenctionmongodb () the     

4, the interpretation of the code

The above is the main code of the program, take out the data from MongoDB according to the requirement, and use the Python script to convert the data into the DataTable type in C #, then convert the DataTable type in C # to Dataframe in Python. The next step is to manipulate the data using the algorithm functions in Python, in which the data is passed back to the C # program using the Python dictionary type. C # Gets the data format is the JSON string, using C # parsing string can get the corresponding data.

C # uses Pythonnet.runtime.dll dynamic Library, the use of the method in PyPI download the Pythonnet module, viewing the use of the document will be explained with detailed usage.

5, the author of the blog writing less, writing level needs to be improved. Because of the limited style of writing, the above code is not a step-by-step interpretation of C # and Python have a better understanding of the people will be easier, but also please forgive the reader, I hope this document to the reader a little inspiration, if the above code there is no place to understand where you can leave a message.

Use of the Pythonnet module to convert a DataTable into a dataframe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.