Python pandas对象使用特殊字符

时间:2017-09-11 15:52:33

标签: python pandas csv special-characters

我有一个pandas数据框,结构如下:

>>> 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

使用itertuples()进行迭代时,特殊字符的列会中断:

>>> 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"应该是" Col.With.Dots"," Col.With。#"," Col.With。%"分别在打印输出中。

我需要将数据帧对象转换为原始字典。所以每个pandas对象都改为dict,如: {'Col1': 'text', 'Col.With.Dots': 111, 'Col.With.#': 111, 'Col.With.%': 111 }

有没有办法克服这个问题?我做了一些研究而无法找到答案

1 个答案:

答案 0 :(得分:1)

使用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'}]

或者,对于循环,请将df.iterrows()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'}
相关问题