使用标头和dtypes将pandas数据帧转换为numpy数组(Converting pandas dataframe to numpy array with headers and dtypes)
我一直在尝试将pandas数据帧转换为numpy数组,并带有dtypes和头名称以便于参考。 我需要这样做,因为对熊猫的处理过于缓慢,numpy快10倍。 我有这个来自SO的代码,它给了我除了我需要的东西,结果看起来不像标准的numpy数组 - 即它没有显示形状中的列数。
[In]: df = pd.DataFrame(randn(10,3),columns=['Acol','Ccol','Bcol']) arr_ip = [tuple(i) for i in df.as_matrix()] dtyp = np.dtype(list(zip(df.dtypes.index, df.dtypes))) dfnp= np.array(arr_ip, dtype=dtyp) print(dfnp.shape) dfnp [Out]: (10,) #expecting (10,3) array([(-1.0645345 , 0.34590193, 0.15063829), ( 1.5010928 , 0.63312454, 2.38309797), (-0.10203999, -0.40589525, 0.63262773), ( 0.92725915, 1.07961763, 0.60425353), ( 0.18905164, -0.90602597, -0.27692396), (-0.48671514, 0.14182815, -0.64240004), ( 0.05012859, -0.01969079, -0.74910076), ( 0.71681329, -0.38473052, -0.57692395), ( 0.60363249, -0.0169229 , -0.16330232), ( 0.04078263, 0.55943898, -0.05783683)], dtype=[('Acol', '<f8'), ('Ccol', '<f8'), ('Bcol', '<f8')])
我错过了什么或者有其他方法吗? 我有很多df要转换,他们的dtypes和列名称不同,所以我需要这种自动化方法。 由于大量的df,我也需要它才能高效。
I have been trying to convert a pandas dataframe into a numpy array, carrying over the dtypes and header names for ease of reference. I need to do this as the processing on pandas is WAY too slow, numpy is 10 fold quicker. I have this code from SO that gives me what I need apart from that the result does not look like a standard numpy array - i.e. it does not show the columns numbers in the shape.
[In]: df = pd.DataFrame(randn(10,3),columns=['Acol','Ccol','Bcol']) arr_ip = [tuple(i) for i in df.as_matrix()] dtyp = np.dtype(list(zip(df.dtypes.index, df.dtypes))) dfnp= np.array(arr_ip, dtype=dtyp) print(dfnp.shape) dfnp [Out]: (10,) #expecting (10,3) array([(-1.0645345 , 0.34590193, 0.15063829), ( 1.5010928 , 0.63312454, 2.38309797), (-0.10203999, -0.40589525, 0.63262773), ( 0.92725915, 1.07961763, 0.60425353), ( 0.18905164, -0.90602597, -0.27692396), (-0.48671514, 0.14182815, -0.64240004), ( 0.05012859, -0.01969079, -0.74910076), ( 0.71681329, -0.38473052, -0.57692395), ( 0.60363249, -0.0169229 , -0.16330232), ( 0.04078263, 0.55943898, -0.05783683)], dtype=[('Acol', '<f8'), ('Ccol', '<f8'), ('Bcol', '<f8')])
Am I missing something or is there another way of doing this? I have many df's to convert and their dtypes and column names vary so I need this automated approach. I also need it to be efficient due to the large number of df's.
原文:https://stackoverflow.com/questions/49734441
最满意答案
我需要指定virtualenv的完整路径:
C:\virtualenvs>C:\python34\Scripts\virtualenv.exe -p C:\Python34\python.exe
因为我有效地称之为:
C:\virtualenvs>C:\python27\Scripts\virtualenv.exe -p C:\Python34\python.exe
因为
C:\python27\Scripts
在我的PATH
。 而Python 2.7和3.4的碰撞导致了这个问题。I needed to specify the full path of virtualenv:
C:\virtualenvs>C:\python34\Scripts\virtualenv.exe -p C:\Python34\python.exe
because I was effectively calling this:
C:\virtualenvs>C:\python27\Scripts\virtualenv.exe -p C:\Python34\python.exe
since
C:\python27\Scripts
is in myPATH
. And the collision of Python 2.7 and 3.4 was causing the issue.
相关问答
更多-
使用pip install virtualenv 。 如果您已安装它,请尝试通过使用pip uninstall virtualenv进行重新安装,然后重新安装。 祝你好运。 Install virtualenv using pip install virtualenv. If you have it already installed, try reinstalling it by removing it with pip uninstall virtualenv and then reinstalling ...
-
这里的问题最终成了一些事情。 正如abarnert在问题的评论中指出的那样,系统python和virtualenv python混合在一起。 为了解决这个问题,我不得不将PyDev项目的项目属性更改为仅指向virtualenv python实例,然后在PyDev解释器首选项中我必须重建PYTHONPATH。 完成此操作后,在virtualenv中我必须运行以下代码: pip uninstall mysql-python pip install mysql-python==1.2.5 这解决了所有问题。 ...
-
简单运行 virtualenv -p python3 envname OP编辑后更新: OP的版本的virtualenv有一个错误,如这里所述。 该问题是通过运行来修复的: pip install --upgrade virtualenv simply run virtualenv -p python3 envname Update after OP's edit: There was a bug in the OP's version of virtualenv, as described here ...
-
在创建virtualenv实例以指定要使用的Python可执行文件时,请使用--python (或short -p )选项,例如: virtualenv --python=/usr/bin/python2.6
NB对于Python 3.6或更高版本,请参阅下面的Aelfinn的答案 。 [编者注:我知道这通常是一个评论,而不是一个编辑,但一个新的评论将被隐藏,我只花了45分钟解开错误,因为这个重要的答案被埋在三四个鹦鹉的答案。 我只是想在这里节省大家时间 ... -
不同Python版本的virtualenv会引发ImportError(virtualenv on different Python version throws ImportError)[2023-07-21]
我需要指定virtualenv的完整路径: C:\virtualenvs>C:\python34\Scripts\virtualenv.exe -p C:\Python34\python.exe 因为我有效地称之为: C:\virtualenvs>C:\python27\Scripts\virtualenv.exe -p C:\Python34\python.exe 因为C:\python27\Scripts在我的PATH 。 而Python 2.7和3.4的碰撞导致了这个问题。 I needed t ... -
我遇到了与你类似的情况,我最终找到了解决办法。 至少在我的情况下,根问题是当我编译python 2.7.8时,构建过程没有找到正确的OpenSSL库(因为它们没有安装在我的系统上)。 完成运行后,它显示了类似于此的摘要: Failed to find the necessary bits to build these modules: _bsddb _curses _curses_panel _hashlib _sqlite3 ...
-
只需下载/ make / install / apt-get / synaptic python 2.7.5并在创建virtulenv时指向它的路径: virtualenv test/test -p /usr/bin/my_2.7.5_directory/python2.7 Just download/make/install/apt-get/synaptic python 2.7.5 and point at it's path like so when creating a virtulenv: vi ...
-
python virtualenv ImportError与芹菜和台球(python virtualenv ImportError with celery and billiard)[2022-02-14]
我能够找出问题所在。 这是ec2-ami附带的默认python27 virtualenv和devel包 对我来说,我不得不更新系统点和系统virutalenv 运用 sudo -i pip install --upgrade pip pip install --upgrade virtualenv 并且我努力运行/ usr / bin / virtualenv来创建一个新的virtualenv。 它解决了我的问题。 希望它也能帮助其他人。 i was able to identify the proble ... -
不,你不能这样混搭。 您需要安装一个针对Python 2.7编译的mod_wsgi版本。 No, you can't mix and match like this. You'll need to install a version of mod_wsgi that is compiled against Python 2.7.
-
尝试使用 #!/usr/bin/env python 作为shebang。 如果这不起作用,请尝试查看which python的输出。 Try using #!/usr/bin/env python as the shebang. If that does not work, try seeing what is the output of which python.