python - Pandas 0.10.1 到 0.11.0 .ix 方法

标签 python pandas

我有一个带有唯一索引的巨大数据框。这是 pandas 0.10.1 中的工作代码,但似乎在 pandas 0.11.0 中中断。

简单地说,我有一个包含 2 列的 DataFrame (df):“Classification”和“A”,两者都填充了数据。df 是唯一索引的。如果“Classification 不在”中,我想覆盖 A 中的值某些列表。

# df contains the original data
accurate_list = ['corr1', 'corr2', 'corr3']
# x is filtered dataframe with only inaccurate entries
x = df[~df.Classification.isin(accurate_list)]
df.ix[x.index,'A'] = df['Classification']

抱歉,无法弄清楚如何在此处获取一些示例数据。问题似乎出在 .ix 方法的最后一行。引用 pandas 0.11.0 whats new documentation http://pandas.pydata.org/pandas-docs/dev/whatsnew.html#v0-11-0-april-22-2013 :

".ix supports mixed integer and label based access. It is primarily label based, but will fallback to integer positional access. .ix is the most general and will support any of the inputs to .loc and .iloc, as well as support for floating point label schemes. .ix is especially useful when dealing with mixed positional and label based hierarchial indexes."

没有异常消息...虽然数据似乎失去了对齐。

如果这是 pandas 的错误或者我在 pandas 0.10.1 中编写了错误的代码,有什么想法吗?

这是一些示例代码。这说明了问题:

accurate_ICB = ['SA EQUITY CFD', 'SA EQUITY', 'SA SSF']
print pd.__version__
data = {'Classification': ['SA EQUITY CFD', 'bbb', 'SA EQUITY', 'SA SSF', 'aaa'],
    'Random': [1,2,3,4,5],
    'X': ['correct', 'wrong','correct', 'correct','wrong']}
df =pd.DataFrame(data)
print "Original DataFrame:"
print df
print "="*35
x = df[~df.Classification.isin(accurate_ICB)]
print x
print "="*35
df.ix[x.index,'X'] = df['Classification']
print df

在 pandas 0.10.1 中它产生:

  Classification  Random        X
0  SA EQUITY CFD       1  correct
1            bbb       2      bbb
2      SA EQUITY       3  correct
3         SA SSF       4  correct
4            aaa       5      aaa

在 pandas 0.11.0 中,右下角的 aaa 变成了 bbb。正在检查的列和正在更改的列之间的任何列似乎都会触发更改的行为

最佳答案

这看起来像一个 bug在 0.11 中,从好的方面来看,它似乎已在 0.11.1 中得到修复(很快就会出来)。

0.11.1.dev-bbcafd8
Original DataFrame:
  Classification  Random        X
0  SA EQUITY CFD       1  correct
1            bbb       2    wrong
2      SA EQUITY       3  correct
3         SA SSF       4  correct
4            aaa       5    wrong
===================================
  Classification  Random      X
1            bbb       2  wrong
4            aaa       5  wrong
===================================
  Classification  Random        X
0  SA EQUITY CFD       1  correct
1            bbb       2      bbb
2      SA EQUITY       3  correct
3         SA SSF       4  correct
4            aaa       5      aaa

关于python - Pandas 0.10.1 到 0.11.0 .ix 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17020763/

相关文章:

python - 带有 ArcGIS 的 Flask 应用程序,Arcpy 不运行

python - 如何将此 Python 代码转换为 C++

python - 我怎样才能有效地半向前/向后填充数据帧中的空白?

python - 数据帧的频率

python - 社区电台上的 GNU Radio

python - 如何读取 IRAF 多规范光谱?

python - 在 Visual Studio Code 中使用 Anaconda

Python 和 Pandas - 按日期排序

python - 如何使用 python-pandas 读取列数不均匀的文本文件?

python - Pandas - 将多个分类列旋转到同一组列中