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
ossphil
V2EX  ›  Python

pandas 读取到的数据和 names 不对应

  •  
  •   ossphil · 2018-03-27 10:00:21 +08:00 · 2222 次点击
    这是一个创建于 2214 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我的文件是这样的: 1UZJa.md.png

    只想读取其中的 5 列:1,3,4,5,6

    在 jupyter notebook 中执行以下代码

    %matplotlib inline
    import matplotlib
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    dtype1 ={'names':('R','c','sigma_r','sigma_t','sigma_h'),'formats':(np.float64, np.float64, np.float64, np.float64, np.float64)}
    
    name2 = {'R','c','sigma_r','sigma_t','sigma_h'}
    dtype2 = {'R': np.float64,'c': np.float64,'sigma_r': np.float64,'sigma_t': np.float64,'sigma_h': np.float64}
    
    a = np.loadtxt('./TS015.dat', dtype=dtype1, usecols=(0,2,3,4,5))
    
    data = pd.read_csv("./TS015.dat",skiprows=2,header=0, sep='\s+', index_col=None, names=name2, dtype=dtype2, usecols=[0,2,3,4,5])
    

    结果发现数组 a 的结果是对的,而 data 里读取的各列的标题是乱的,并不是我想要的‘ R ’对应于第一列,‘ c ’对应于第二列。不知道是不是 read_csv 函数有什么参数没有设置对?

    使用的环境是 Ubuntu 下通过 pyenv 安装的 anaconda3-4.3.1,notebook 信息: Server Information:

    You are using Jupyter notebook.

    The version of the notebook server is: 5.4.1-bcc8ab2 The server is running on this version of Python:

    Python 3.6.4 | packaged by conda-forge | (default, Dec 23 2017, 16:31:06) 
    [GCC 4.8.2 20140120 (Red Hat 4.8.2-15)]
    

    Current Kernel Information:

    Python 3.6.4 | packaged by conda-forge | (default, Dec 23 2017, 16:31:06) 
    Type 'copyright', 'credits' or 'license' for more information
    IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
    

    我另外在 ubuntu17.10 的虚拟机里尝试了同样的代码,read_csv 读取到的结果也不对,names 和列似乎是随意匹配的一样。。。

    请各位指正,先谢过。

    4 条回复    2018-03-27 10:43:19 +08:00
    imn1
        1
    imn1  
       2018-03-27 10:19:53 +08:00   ❤️ 1
    我记得 names 需要 list,也就是有序,你这个 set 是无序的
    kingmo888
        2
    kingmo888  
       2018-03-27 10:33:52 +08:00
    set 本身就是无序的。。
    zhusimaji
        3
    zhusimaji  
       2018-03-27 10:38:33 +08:00
    names 要使用 list 类型,保证有序,楼上两个已经说得清楚了。。。
    ossphil
        4
    ossphil  
    OP
       2018-03-27 10:43:19 +08:00
    @imn1
    @kingmo888
    @zhusimaji
    惭愧,改成 name2 = ['R','c','sigma_r','sigma_t','sigma_h']就对了。原来把列表写成字典类型了。对 python 完全没有了解,现照着例子做的,犯了这样的低级错误。非常感谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4669 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 04:04 · PVG 12:04 · LAX 21:04 · JFK 00:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.