我们可以在 .NET Ironpython 中加载 Pandas DataFrame 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14432059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we load pandas DataFrame in .NET ironpython?
提问by Alok
Can we load a pandas DataFrame in .NET space using iron python? If not I am thinking of converting pandas df into a csv file and then reading in .net space.
我们可以使用铁蟒在 .NET 空间中加载 Pandas DataFrame 吗?如果不是,我正在考虑将 pandas df 转换为 csv 文件,然后在 .net 空间中读取。
采纳答案by Jeff Hardy
No, Pandas is pretty well tied to CPython. Like you said, your best bet is to do the analysis in CPython with Pandas and export the result to CSV.
不,Pandas 与 CPython 有很好的联系。就像你说的,你最好的办法是用 Pandas 在 CPython 中进行分析并将结果导出到 CSV。
回答by Rafal Zajac
Regarding the option including serialization:
关于包括序列化的选项:
I'm still investigating similar case - we want to process the data in python and then use the results in c#. Our requirement was to (preferably) keep the python part platform independent so that we can run our number crunching on either linux or windows. Long story short we decided to use binary serialization/deserialization with Message Pack: http://msgpack.org/index.html
我仍在调查类似的案例——我们想在 python 中处理数据,然后在 c# 中使用结果。我们的要求是(最好)保持 python 部分平台独立,以便我们可以在 linux 或 windows 上运行我们的数字运算。长话短说,我们决定对 Message Pack 使用二进制序列化/反序列化:http: //msgpack.org/index.html
We convert the DataFrame values to list, and serialize to file:
我们将 DataFrame 值转换为列表,并序列化为文件:
import msgpack as mp
data_as_list = df.values.tolist()
mp.pack(data_as_list, open("d:\msgpack1.mp",'wb'))
Then on the C# side we use the .net implementation of MessagePack to deserialize the data:
然后在 C# 端,我们使用 MessagePack 的 .net 实现来反序列化数据:
using MsgPack;
var serializer =
SerializationContext.Default.GetSerializer<MessagePackObject[][]>();
var unpackedObject = serializer.Unpack(File.OpenRead("d:\msgpack1.mp"));
Main advantages of binary serialization:
二进制序列化的主要优点:
- is less prone to any encoding related issues comparing to text based serialization formats like csv, json or xml
- depending on the data it can be faster than CSV (it was in our case):http://matthewrocklin.com/blog/work/2015/03/16/Fast-Serialization/
- 与基于文本的序列化格式(如 csv、json 或 xml)相比,不太容易出现任何与编码相关的问题
- 根据数据,它可以比 CSV 更快(在我们的例子中是这样):http: //matthewrocklin.com/blog/work/2015/03/16/Fast-Serialization/
回答by denfromufa
It is possible to call CPython from .NET using Python.NET:
可以使用 Python.NET 从 .NET 调用 CPython:

