根据MultiIndex Pandas填充NaN(Fill NaN based on MultiIndex Pandas)
我有一个大熊猫数据框,我想填写一些NaN值。
import pandas as pd tuples = [('a', 1990),('a', 1994),('a',1996),('b',1992),('b',1997),('c',2001)] index = pd.MultiIndex.from_tuples(tuples, names = ['Type', 'Year']) vals = ['NaN','NaN','SomeName','NaN','SomeOtherName','SomeThirdName'] df = pd.DataFrame(vals, index=index) print(df) 0 Type Year a 1990 NaN 1994 NaN 1996 SomeName b 1992 NaN 1997 SomeOtherName c 2001 SomeThirdName
我想要的输出是:
Type Year a 1990 SomeName 1994 SomeName 1996 SomeName b 1992 SomeOtherName 1997 SomeOtherName c 2001 SomeThirdName
这需要在更大的DataFrame(数百万行)上完成,其中每个'Type'可以具有1-5个唯一的'Years',并且名称值仅出现在最近一年。 我试图避免为了性能目的迭代行。
I have a pandas Data Frame that I would like to fill in some NaN values of.
import pandas as pd tuples = [('a', 1990),('a', 1994),('a',1996),('b',1992),('b',1997),('c',2001)] index = pd.MultiIndex.from_tuples(tuples, names = ['Type', 'Year']) vals = ['NaN','NaN','SomeName','NaN','SomeOtherName','SomeThirdName'] df = pd.DataFrame(vals, index=index) print(df) 0 Type Year a 1990 NaN 1994 NaN 1996 SomeName b 1992 NaN 1997 SomeOtherName c 2001 SomeThirdName
The output that I would like is:
Type Year a 1990 SomeName 1994 SomeName 1996 SomeName b 1992 SomeOtherName 1997 SomeOtherName c 2001 SomeThirdName
This needs to be done on a much larger DataFrame (millions of rows) where each 'Type' can have between 1-5 unique 'Years' and the name value is only present for the most recent year. I'm trying to avoid iterating over rows for performance purposes.
原文:https://stackoverflow.com/questions/38961234
最满意答案
根据我与MathWorks支持的沟通,MATLAB Engine for Python与Anaconda不兼容。 幸运的是,这是不正确的。
感谢Anaconda邮件列表的一些指示,我找到了一个简单的解决方案:
sudo ln -s ~/anaconda/lib/libpython3.4m.dylib /usr/lib
在此之后,matlab.engine成功导入。 希望这有助于某人!
编辑:从OS X 10.11 El Capitan开始,需要根据Rootless功能调整解决方法,因为root不再具有修改/ usr / lib的权限
sudo ln -s ~/anaconda/lib/libpython3.4m.dylib /usr/local/lib
According to my communication with MathWorks support, MATLAB Engine for Python is incompatible with Anaconda. Fortunately, this is incorrect.
Thanks to some direction from the Anaconda mailing list, I found a simple solution:
sudo ln -s ~/anaconda/lib/libpython3.4m.dylib /usr/lib
After this, matlab.engine imports successfully. Hope this helps someone!
Edit: As of OS X 10.11 El Capitan, the workaround needs to be tweaked due to the Rootless feature, as root no longer has permission to modify /usr/lib
sudo ln -s ~/anaconda/lib/libpython3.4m.dylib /usr/local/lib
相关问答
更多-
请参阅此处的答案: 安装tensorflow时出现错误setuptools 特别是,您可以尝试将--ignore-installed标记添加到pip命令的末尾。 See the answer here: Error setuptools when installing tensorflow In particular, you could try adding the --ignore-installed tag to the end of your pip command.
-
自己做了。 只需将在matlab目录中为py2.7生成的matlab文件夹复制到我的anaconda的virtual-env的站点包中。 根据上面提到的路径,您需要在linux终端上执行此操作。 cp /usr/local/MATLAB/R2016a/extern/engines/python/build/lib.linux-x86_64-2.7/matlab /home/fire-trail/anaconda3/envs/py34/lib/python3.4 它将在python中用于蟒蛇。 请记住,lin ...
-
Python的Matlab引擎安装(Python Matlab Engine Install)[2023-08-26]
查看Matlab 2014的发行说明,Python引擎在R2014b之前不存在,因此我认为您不能在2012年使用它。 http://uk.mathworks.com/help/matlab/release-notes.html Looking at the release notes for Matlab 2014, the Python engine didn't exist until R2014b, so I don't think you'll be able to use it in 2012. ... -
在anaconda lib文件夹中检查到它存在libpython3.5库之后,我只是在MATLAB pyversion 3.5输入,并解决了这个问题。 After having checked that in the anaconda lib folder it was present the libpython3.5 library, I simply typed in MATLAB pyversion 3.5, and this fixed the problem.
-
您可以尝试找到在binstar上安装此软件包的人 ,该软件是Anaconda的软件包托管站点。 我发现https://binstar.org/dhirschfeld/pyodbc/3.0.7/files ,如果你有64位Windows,它可能适合你。 你可以试试conda install -c https://conda.binstar.org/dhirschfeld pyodbc 。 You could try to find someone who's built this package on bin ...
-
使用Anaconda安装“MATLAB Engine for Python”的问题(Problems installing “MATLAB Engine for Python” with Anaconda)[2023-04-28]
根据我与MathWorks支持的沟通,MATLAB Engine for Python与Anaconda不兼容。 幸运的是,这是不正确的。 感谢Anaconda邮件列表的一些指示,我找到了一个简单的解决方案: sudo ln -s ~/anaconda/lib/libpython3.4m.dylib /usr/lib 在此之后,matlab.engine成功导入。 希望这有助于某人! 编辑:从OS X 10.11 El Capitan开始,需要根据Rootless功能调整解决方法,因为root不再具有修改 ... -
自从使用Python 3.6以来,我已经看到了很多。 我发现,在收到这封消息后,经常使用pip来安装一个包就可以了。 就像这种情况一样。 我刚刚测试过: Python 3.6.3 :: Anaconda custom(x86_64) pip install parmap Collecting parmap Downloading parmap-1.5.1-py2.py3-none-any.whl Installing collected packages: parmap Successfully ins ...
-
找到答案: import pandas失败,因为matplotlib无法初始化,因为它试图获取当前用户的主目录。 由于应用程序是通过WSGI运行的,因此HOME变量设置为/home/wsgi 但该目录不存在 。 所以,通过部署命令创建这个目录解决了这个问题。 我在Elastic Beanstalk上使用Anaconda的整体设置如下: .ebextensions/options.config包含: 命令: 00_download_conda: 命令:'wget http://repo.continuum.i ...
-
安装后如何让Anaconda3看到Python 2.7?(How do I get Anaconda3 to see Python 2.7 after installing it?)[2022-01-11]
在Windows上,访问二进制文件时,当前路径优先于PATH变量。 因此,由于您仍然在包含python.exe的Anaconda3目录中, cmd将选择那个而不是通过PATH查找它,这将返回Python 2.7可执行文件。 cd到不同的路径,你很好。 On Windows the current path takes precedence over the PATH variable when accessing binaries. Thus, since you are still in the Anac ... -
使用Anaconda安装包(Installing packages with Anaconda)[2021-12-22]
你可以在这里尝试类似的答案: https : //stackoverflow.com/a/33698750/5573572 几乎完成以下步骤: 1. Uninstall TensorFlow from pip: pip uninstall tensorflow 做以上操作以避免冲突。 2. Install Python 3 in a virtual environment (version 0.7.1 as of this writing): conda create -n相关文章
更多- Java-based 实现的索引复制
- JasperReport填充报表
- R语言实战视频教程-尚学堂视频教程
- Ext grid分页的问题
- jquery autocomplete实现solr查询字段自动填充并执行查询
- 左则导航鼠标滑过时显示详情的实现
- 基于lucene实现自己的推荐引擎
- 基于lucene实现自己的推荐引擎
- 基于lucene实现自己的推荐引擎
- [How to] Make custom search with Nutch(v 1.0)?(转)
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)