dranxo
dranxo

Reputation: 3388

Removing a pandas column with a unicode name

edit: the problem isn't unicode related

I have columns with unicode names, eg "#前田敦子:Japan". I'd like to remove these from my dataframe, but I'm getting a UnicodeEncodeError:

This code

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

gives this error

  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)

I can .drop just fine when the column name has no unicode, any ideas?

Upvotes: 0

Views: 623

Answers (1)

BrenBarn
BrenBarn

Reputation: 251408

There are a few problems here. drop by default operates on rows, not columns. Also, by default it returns a new object and doesn't modify the original. The problem may be due to it trying to print out the remaining columns, some of which also contain undisplayable Unicode.

Upvotes: 1

Related Questions