Creating h2o dataframe from pandas and unicode error
如何安全地将pandas对象转换为H2O数据帧?
1 2 3 4 5 | import h2o import pandas as pd df = pd.DataFrame({'col1': [1,1,2], 'col2': ['César Chávez Day', 'César Chávez Day', 'César Chávez Day']}) hf = h2o.H2OFrame(df) #gives error |
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in
position 4: ordinal not in range(128)
环境:python 3.5,h2o 3.10.4.2
我同意这不是特定于H2O的问题。这对我有效(同样的H2O和Python版本):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import h2o import pandas as pd df = pd.DataFrame({'col1': [1,1,2], 'col2': ['César Chávez Day', 'César Chávez Day', 'César Chávez Day']}) hf = h2o.H2OFrame(df) ## -- End pasted text -- Parse progress: |█████████████████████████████████████████████████████████| 100% In [4]: hf Out[4]: col1 col2 ------ ---------------- 1 César Chávez Day 1 César Chávez Day 2 César Chávez Day [3 rows x 2 columns] In [5]: type('César Chávez Day') Out[5]: str |
我的规格(您可能需要更改默认编码):
1 2 3 4 | In [6]: import sys In [7]: sys.getdefaultencoding() Out[7]: 'utf-8' |
这个线程可能有帮助:如何检查字符串是Unicode还是ASCII?