使用unicode名称删除pandas列

时间:2014-03-20 02:04:18

标签: python unicode pandas

编辑:问题与unicode无关

我有带有unicode名称的列,例如“#前田敦子:日本”。我想从我的数据框中删除这些内容,但我得到的是UnicodeEncodeError

此代码

for title_loc in f_df.columns:
    if 'Japan' in title_loc:
        f_df.drop(title_loc)

发出此错误

  File "timeseries/sum_japan_timeseries.py", line 25, in read_timeseries_dir
    f_df.drop(title_loc)
  File "/home/isl/rfcompton/.local/lib/python2.7/site-packages/pandas/core/generic.py", line 1401, in drop
    new_axis = axis.drop(labels)
  File "/home/isl/rfcompton/.local/lib/python2.7/site-packages/pandas/core/index.py", line 1623, in drop
    raise ValueError('labels %s not contained in axis' % labels[mask])
  File "/usr/local/python/2.7.2/lib/python2.7/site-packages/numpy-1.6.1-py2.7-linux-x86_64.egg/numpy/core/numeric.py", line 1379, in array_str
    return array2string(a, max_line_width, precision, suppress_small, ' ', "", str)
  File "/usr/local/python/2.7.2/lib/python2.7/site-packages/numpy-1.6.1-py2.7-linux-x86_64.egg/numpy/core/arrayprint.py", line 309, in array2string
    separator, prefix)
  File "/usr/local/python/2.7.2/lib/python2.7/site-packages/numpy-1.6.1-py2.7-linux-x86_64.egg/numpy/core/arrayprint.py", line 230, in _array2string
    _summaryEdgeItems, summary_insert)[:-1]
  File "/usr/local/python/2.7.2/lib/python2.7/site-packages/numpy-1.6.1-py2.7-linux-x86_64.egg/numpy/core/arrayprint.py", line 355, in _formatArray
    word = format_function(a[-1])
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-3: ordinal not in range(128)

当列名没有unicode,任何想法时,我可以.drop就好了吗?

1 个答案:

答案 0 :(得分:1)

这里有一些问题。默认情况下,drop对行而不是列进行操作。此外,默认情况下,它返回一个新对象,并且不会修改原始对象。问题可能是由于它试图打印出剩余的列,其中一些列还包含不可显示的Unicode。