gpt4 book ai didi

python - 属性错误: 'DataFrame' object has no attribute 'ravel'

转载 作者:太空宇宙 更新时间:2023-11-03 17:42:49 25 4
gpt4 key购买 nike

我正在尝试测试 panda 程序,该程序在其他系统上运行良好。

import pandas as pd
import numpy as np
from pandas import Series, DataFrame
ds1 = pd.read_table('data.txt', sep=' ', header=None)
ds2 = pd.read_table('Dataset.txt', header=None, sep=' ')
out = ds1.copy()

_,C = np.where(ds1.ravel()[:,None] == ds2[:,0])

newvals = ds2[C,1]

# Valid positions in output array to be changed
valid = np.in1d(ds1.ravel(),ds2[:,0])

# Finally make the changes to get desired output
out.ravel()[valid] = newvals
print out

当我尝试时,它给出了,

    _,C = np.where(ds1.ravel()[:,None] == ds2[:,0])
File "/usr/local/lib/python2.7/dist-packages/pandas/core/generic.py", line 1947, in __getattr__
(type(self).__name__, name))
AttributeError: 'DataFrame' object has no attribute 'ravel'

更新

原始数据示例:

ds1 = pd.read_table('https://gist.githubusercontent.com/karimkhanp/9527bad750fbe75e072c/raw/ds1', sep=' ', header=None)
ds2 = pd.read_table('https://gist.githubusercontent.com/karimkhanp/1692f1f76718c35e939f/raw/6f6b348ab0879b702e1c3c5e362e9d2062e9e9bc/ds2', header=None, sep=' ')

最佳答案

错误非常明显,您想要访问 numpy values 属性:

_,C = np.where(ds1.values.ravel()[:,None] == ds2[:,0])

关于python - 属性错误: 'DataFrame' object has no attribute 'ravel' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30258246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com