Chen A.
Chen A.

Reputation: 11280

Python pandas object converts column names with special characters

I have a pandas dataframe structured like:

>>> df
   Col1  Col.With.Dots  Col.With.#  Col.With.%
0  text            111         111         111
1  text            222         222         222
2  text            333         333         333
3  text            444         444         444
4  text            555         555         555

When iterating over it with itertuples(), the columns with special characters break:

>>> for i in df.itertuples():
...    print i

Pandas(Index=0, Col1='text', _2=111, _3=111, _4=111)
Pandas(Index=1, Col1='text', _2=222, _3=222, _4=222)
Pandas(Index=2, Col1='text', _2=333, _3=333, _4=333)
Pandas(Index=3, Col1='text', _2=444, _3=444, _4=444)
Pandas(Index=4, Col1='text', _2=555, _3=555, _4=555)

"_2", "_3", "_4" should be "Col.With.Dots", "Col.With.#", "Col.With.%" in the print output, respectively.

I need to convert the dataframe object to a raw dict. So every pandas object is changed to a dict such: {'Col1': 'text', 'Col.With.Dots': 111, 'Col.With.#': 111, 'Col.With.%': 111 }

Is there a way to overcome this? I did some research and couldn't find an answer

Upvotes: 3

Views: 1568

Answers (1)

Zero
Zero

Reputation: 76927

Use to_dict()

In [1659]: df.to_dict('r')
Out[1659]:
[{'Col.With.#': 111L, 'Col.With.%': 111L, 'Col.With.Dots': 111L, 'Col1': 'text'},
 {'Col.With.#': 222L, 'Col.With.%': 222L, 'Col.With.Dots': 222L, 'Col1': 'text'},
 {'Col.With.#': 333L, 'Col.With.%': 333L, 'Col.With.Dots': 333L, 'Col1': 'text'},
 {'Col.With.#': 444L, 'Col.With.%': 444L, 'Col.With.Dots': 444L, 'Col1': 'text'},
 {'Col.With.#': 555L, 'Col.With.%': 555L, 'Col.With.Dots': 555L, 'Col1': 'text'}]

Or, for looping, use df.iterrows() with to_dict()

In [1667]: for i, x in df.iterrows():
      ...:     print x.to_dict()
      ...:
{'Col.With.%': 111L, 'Col.With.Dots': 111L, 'Col.With.#': 111L, 'Col1': 'text'}
{'Col.With.%': 222L, 'Col.With.Dots': 222L, 'Col.With.#': 222L, 'Col1': 'text'}
{'Col.With.%': 333L, 'Col.With.Dots': 333L, 'Col.With.#': 333L, 'Col1': 'text'}
{'Col.With.%': 444L, 'Col.With.Dots': 444L, 'Col.With.#': 444L, 'Col1': 'text'}
{'Col.With.%': 555L, 'Col.With.Dots': 555L, 'Col.With.#': 555L, 'Col1': 'text'}

Upvotes: 1

Related Questions