我正在尝试测试在其他系统上运行良好的熊猫程序。
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=' ')
答案 0 :(得分:1)
错误很明显,您想要访问numpy values
属性:
_,C = np.where(ds1.values.ravel()[:,None] == ds2[:,0])