复制带有pd.read_clipboard的MultiIndex数据帧? [英] Copying MultiIndex dataframes with pd.read_clipboard?

查看:52
本文介绍了复制带有pd.read_clipboard的MultiIndex数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个这样的数据框:

          C
A   B      
1.1 111  20
    222  31
3.3 222  24
    333  65
5.5 333  22
6.6 777  74 

如何使用pd.read_clipboard阅读它?我已经尝试过了:

How do I read it in using pd.read_clipboard? I've tried this:

df = pd.read_clipboard(index_col=[0, 1])

但是它会引发错误:

ParserError: Error tokenizing data. C error: Expected 2 fields in line 3, saw 3

我该如何解决?

其他pd.read_clipboard问题:

推荐答案

更新:现在它解析剪贴板-即无需事先保存

UPDATE: now it parses the clipboard - i.e. no need to save it beforehand

def read_clipboard_mi(index_names_row=None, **kwargs):
    encoding = kwargs.pop('encoding', 'utf-8')

    # only utf-8 is valid for passed value because that's what clipboard
    # supports
    if encoding is not None and encoding.lower().replace('-', '') != 'utf8':
        raise NotImplementedError(
            'reading from clipboard only supports utf-8 encoding')

    from pandas import compat, read_fwf
    from pandas.io.clipboard import clipboard_get
    from pandas.io.common import StringIO
    data = clipboard_get()

    # try to decode (if needed on PY3)
    # Strange. linux py33 doesn't complain, win py33 does
    if compat.PY3:
        try:
            text = compat.bytes_to_str(
                text, encoding=(kwargs.get('encoding') or
                                get_option('display.encoding'))
            )
        except:
            pass

    index_names = None
    if index_names_row:
        if isinstance(index_names_row, int):
            index_names = data.splitlines()[index_names_row].split()
            skiprows = [index_names_row]
            kwargs.update({'skiprows': skiprows})
        else:
            raise Exception('[index_names_row] must be of [int] data type')

    df = read_fwf(StringIO(data), **kwargs)
    unnamed_cols = df.columns[df.columns.str.contains(r'Unnamed:')].tolist()

    if index_names:
        idx_cols = df.columns[range(len(index_names))].tolist()
    elif unnamed_cols:
        idx_cols = df.columns[range(len(unnamed_cols))].tolist()
        index_names = [None] * len(idx_cols)

    df[idx_cols] = df[idx_cols].ffill()
    df = df.set_index(idx_cols).rename_axis(index_names)

    return df

测试没有索引名称的多索引DF:

testing multi-index DF without index names:

In [231]: read_clipboard_mi()
Out[231]:
          C
1.1 111  20
    222  31
3.3 222  24
    333  65
5.5 333  22
6.6 777  74

使用索引名称测试多索引DF:

testing multi-index DF with index names:

In [232]: read_clipboard_mi(index_names_row=1)
Out[232]:
          C
A   B
1.1 111  20
    222  31
3.3 222  24
    333  65
5.5 333  22
6.6 777  74

注意:

  1. 它没有经过良好的测试
  2. 它不支持多级列
  3. 请参阅第1点;-)

注意2::请随时使用此代码或创建 a在Pandas github上的拉取请求

NOTE2: please feel free to use this code or to create a pull request on Pandas github

这篇关于复制带有pd.read_clipboard的MultiIndex数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆