- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
给定一个 dataframe like this :
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
读取它?我试过这个:
df = pd.read_clipboard(index_col=[0, 1])
但是它抛出一个错误:
ParserError: Error tokenizing data. C error: Expected 2 fields in line 3, saw 3
我该如何解决这个问题?
最佳答案
更新:现在它解析剪贴板 - 即无需事先保存
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:
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:
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
注意:
注意 2:请随意使用此代码或创建 a pull request on Pandas github
关于python - 使用 pd.read_clipboard 复制 MultiIndex 数据帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45740537/
我有一个包含 4 个级别的 MultiIndex 的 pandas 数据框。我正在尝试为每个 1 级索引选择具有不同 4 级索引的行。 例子: In [68]: df = pd.DataFrame({
我有一个数据框,其中包含 Year 和 Month 的多个索引,如下所示 | |Value Year |Month| | 1 | 3 1992 | 2 | 5
我有一个带有 3 级 MultiIndex 的 pandas 数据框。我正在尝试根据对应于两个级别的值列表提取此数据帧的行。 我有这样的事情: ix = pd.MultiIndex.from_prod
我想将具有多索引的数据帧中的所有列除以另一个具有多索引的数据帧,更小一级。前两个水平的蛾指数是相同的。并且应该播放第三级。 df_0 = pd.DataFrame( { "col0": [ 1
我想知道是否有人可以帮助我解决这个问题。 如果我有一个简单的数据框: one two three four 0 A 1 a 1 1 A 2 b
我希望创建一个新的 DataFrame,与基于芯片的设备 A 和 B 的结果相对应。 以下是我创建 DataFrame 的代码: import numpy as np import pandas as
我希望创建一个新的 DataFrame,与基于芯片的设备 A 和 B 的结果相对应。 以下是我创建 DataFrame 的代码: import numpy as np import pandas as
请让我知道如何将两个 DataFrame 与排序的 MultiIndexes 连接起来,以便结果具有排序的 MultiIndex。 由于两者都是排序的,算法必须根据两个 DataFrame 中的总行数
基本场景 对于推荐服务,我正在针对一组用户-项目交互训练矩阵分解模型 (LightFM)。为了使矩阵分解模型产生最佳结果,我需要将我的用户 ID 和商品 ID 映射到从 0 开始的连续整数 ID 范围
好的,所以我有一个数据框,其中包含时间序列数据,每列都有一个多行索引。这是数据的样例,它是 csv 格式的。加载数据在这里不是问题。 我想要做的是能够创建一个箱线图,其中包含根据多索引特定行中的不同类
我有这个数据框: df = pd.DataFrame({'NUMBER_1': {('2019-07', 'A'): 4, ('2019-07', 'D'): 2, ('2019-08', 'A'):
我有点难以对用于我的一个数据集的列的 pandas MultiIndex 进行排序: MultiIndex(levels=[['Jan', 'Feb', 'Mar', 'Apr', 'May', 'J
我有点难以对用于我的一个数据集的列的 pandas MultiIndex 进行排序: MultiIndex(levels=[['Jan', 'Feb', 'Mar', 'Apr', 'May', 'J
我有一个列表列表,我想将其制成多索引 Pandas 数据框,然后可以合并到原始 Pandas 数据框。列表的每一行都包含一个观察值,列表中的值由与该观察值匹配的相应行组成。 这是我所拥有的一个简单版本
我有以下 DataFrame(从我无法更改的 CSV 文件中读取): df = pd.DataFrame([['low', 0.5, 123, 0.8, 123],
我正在创建一个 MultiIndex.from_product(),但它必须是来自两个单独的 MultiIndexes 的唯一值的乘积。我下面的解决方案有效,但我想知道是否有更优雅的解决方案。 fro
我有一个很大的pd.DataFrame,a,看起来像: bid TIT IM Equity HELN SE Equity FHZN SE Equity GLEN LN Equity f
我创建了一个带有多索引的数据框,其中一个索引是一个元组。我无法使用 .loc 进行索引。 这是数据框: a = [['a','a','a','b','b','b','c','c'],[('one',2
我有以下数据框 A B C D E F Algo T X
我在使用 MultiIndex 和 stack() 时遇到问题。以下示例基于 solution from Calvin Cheung在 StackOvervlow 上。 === multi.csv =
我是一名优秀的程序员,十分优秀!