V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
youthfire
V2EX  ›  Python

Pandas Dataframe 的 out of bounds 问题

  •  
  •   youthfire · 2018-12-25 23:38:53 +08:00 · 1401 次点击
    这是一个创建于 1942 天前的主题,其中的信息可能已经有所发展或是发生改变。

    假设原有的每个独立 csv 是 9 个 column,后期的每个独立 csv 增加了一列,即 10 个 column

    通过 pandas 读取所有新老 csv df = pd.DataFrame(pd.read_csv(file))

    希望取到第 10 个 column 的数值,如果是老的 csv,该增加列可以取空值。

    在利用 df.iloc 取值时,对于老的 csv,会报 out of bounds 错误 老的 csv 文件也有数千个

    求教有好的解决方案吗?

    2 条回复    2018-12-26 10:34:02 +08:00
    necomancer
        1
    necomancer  
       2018-12-26 09:44:10 +08:00   ❤️ 1
    用 get(),如果列有名字则用名字,否则用序号,get 如果没有则返回 None,方便处理。

    In [2]: import pandas as pd

    In [3]: df = pd.DataFrame(np.random.randn(8, 4))

    In [4]: df
    Out[4]:
    0 1 2 3
    0 -0.727670 -0.182557 -0.957270 -0.153352
    1 -0.340649 -0.313155 -1.219515 0.082472
    2 0.023527 0.496896 0.443117 -0.391405
    3 -0.522745 0.879736 -1.358356 0.177883
    4 -0.314936 -1.795936 -1.510872 1.039757
    5 0.000243 -0.826999 -0.365514 -0.907249
    6 0.058694 -0.521912 -0.863121 0.842308
    7 0.846951 0.325337 -0.821745 0.111492

    In [5]: df.get(0)
    Out[5]:
    0 -0.727670
    1 -0.340649
    2 0.023527
    3 -0.522745
    4 -0.314936
    5 0.000243
    6 0.058694
    7 0.846951
    Name: 0, dtype: float64

    In [6]: df.get(1)
    Out[6]:
    0 -0.182557
    1 -0.313155
    2 0.496896
    3 0.879736
    4 -1.795936
    5 -0.826999
    6 -0.521912
    7 0.325337
    Name: 1, dtype: float64

    In [7]: df.get(3)
    Out[7]:
    0 -0.153352
    1 0.082472
    2 -0.391405
    3 0.177883
    4 1.039757
    5 -0.907249
    6 0.842308
    7 0.111492
    Name: 3, dtype: float64

    In [8]: df.get(4)

    In [9]:

    或者列有名字:

    In [10]: df = pd.DataFrame(np.random.randn(8, 4), columns=['A', 'B', 'C', 'D'])

    In [11]: df
    Out[11]:
    A B C D
    0 -1.521750 -0.704144 -0.565343 -0.389537
    1 -0.634391 0.672338 0.857965 0.294724
    2 -0.764034 0.907585 -1.454368 -0.637835
    3 -1.218633 -1.473434 1.441891 1.554465
    4 -1.100643 -2.303968 -1.788275 -0.382192
    5 1.476041 -0.735864 -0.359389 0.896467
    6 1.662332 -0.944238 0.308855 -0.013283
    7 1.357332 0.529256 1.169877 0.745932

    In [12]: df.get('E')

    In [13]: df.get('B')
    Out[13]:
    0 -0.704144
    1 0.672338
    2 0.907585
    3 -1.473434
    4 -2.303968
    5 -0.735864
    6 -0.944238
    7 0.529256
    Name: B, dtype: float64

    实在不行还可以用 try except 吧。
    zhusimaji
        2
    zhusimaji  
       2018-12-26 10:34:02 +08:00   ❤️ 1
    楼上的方法 get 是不错的方法,还有就是可以判断 dataframe 的 shape 大小,针对列大小不一致的情况你处理下就好了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1107 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:33 · PVG 02:33 · LAX 11:33 · JFK 14:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.