DataFrame.from_dict生成列和行熊猫python

时间:2019-10-31 13:32:50

标签: python pandas dataframe

我有以下代码

df_output ={'transport':[transport],'model':[model],fuel':[fuel],'engine':[engine],'color':[color],'year':[year],'value':[value],'
number':[number],'manufacturer': [manufacturer],}

我通过df_output以便能够在DataFrame.from_dict中进行操作

df = pd.DataFrame.from_dict(data=df_output, orient='index', columns=['transport', 'Model', 'Fuel', 'Engine', 'Color', 'Year', 'Value', 'Number','Manufacturer'])

我正在关注pandas文档

他告诉我使用orient = 'index',以便我可以手动创建列,然后传递列columns=['transport', 'Model', 'Fuel', 'Engine', 'Color', 'Year', 'Value', 'Number','Manufacturer']

当我执行我的代码时,它会产生此错误

Traceback (most recent call last):
  File "C:\Users\Suporte\Desktop\Captura TJ\autonomation\Discovery.py", line 192, in <module>
    Scrap(user_tj, password_tj, pesquisa_unica)
  File "C:\Users\Suporte\Desktop\Captura TJ\autonomation\Discovery.py", line 54, in __init__
    self.single_search_csv()
  File "C:\Users\Suporte\Desktop\Captura TJ\autonomation\Discovery.py", line 182, in single_search_csv
    df = pd.DataFrame.from_dict(data=df_output, orient='index', columns=['transport', 'Model', 'Fuel', 'Engine', 'Color', 'Year', 'Value', 'Number','Manufacturer'])
  File "C:\Users\Suporte\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 1190, in from_dict
    return cls(data, index=index, columns=columns, dtype=dtype)
  File "C:\Users\Suporte\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 450, in __init__
    arrays, columns = to_arrays(data, columns, dtype=dtype)
  File "C:\Users\Suporte\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\construction.py", line 464, in to_arrays
    return _list_to_arrays(data, columns, coerce_float=coerce_float, dtype=dtype)
  File "C:\Users\Suporte\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\construction.py", line 503, in _list_to_arrays
    raise ValueError(e) from e
ValueError: 9 columns passed, passed data had 1 columns

一切顺利后,我将生成一个csv

df.to_csv('file.csv', sep = ';', encoding = 'iso-8859-1', mode='a', index=False

谁能帮助我谢谢你:)

1 个答案:

答案 0 :(得分:0)

创建 df_output 时,请在每个值周围放置方括号。

我假设 transport model 等都是列表。 最初的编写方式不必要地创建另一组列表, 每个都只包含一个元素(一个列表)。

因此将说明更改为:

df_output = {'transport': transport, 'model': model, ...}