首页 > 解决方案 > 如何在不将键转换为列表的情况下将 2 列数据框转换为字典

问题描述

我会先说我是一个非常业余的用户,虽然我已经广泛研究了我的问题,但我还没有找到解决方案。我认为解决方案很简单,但我们会看到。

简化后,我有一个列名为 A、B、C、D 等的数据框,我想将这些名称更改为 a、b、c、d 等。列名列表很长,以便实现这些我已经从一个包含 2 列的 excel 文件中导入了一个数据框(我在这里使用了 excel,因为我想为我正在创建的整个程序创建一个易于重现的方法)。第一列有 A、B、C、D...,第二列有 a、b、c、d。

然后我拿了这个数据框,将索引设置为第 0 列,然后转置它。然后我使用.to_dict('list'),结果字典看起来几乎是正确的,除了值在列表中:{'A':['a'], 'B':['b']...}. 所以当我尝试执行时,df.rename(columns=dictionary)我得到了不可散列的类型列表错误。

我知道这是因为我的值存储为列表,如果字典看起来像{'A':'a', 'B':'b'...}我打赌它会正常工作。

所以基本上,我如何将我的数据框变成一个没有这样格式列表的字典?或者这是不可能的,我应该以不同的方式解决这个问题?

谢谢!

这是我的实际代码:

输入

df_plate = pd.read_excel('plate.xlsx',index_col='sample')
df_plate_t = df_plate.T
dict_plate = df_plate_t.to_dict('list')
df_sorted2 = df_sorted.rename(columns=dict_plate)
df_sorted2

输出

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

~\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\lib\pretty.py in pretty(self, obj)
    400                         if cls is not object \
    401                                 and callable(cls.__dict__.get('__repr__')):
--> 402                             return _repr_pprint(obj, self, cycle)
    403 
    404             return _default_pprint(obj, self, cycle)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\lib\pretty.py in _repr_pprint(obj, p, cycle)
    695     """A pprint that just redirects to the normal repr function."""
    696     # Find newlines and replace them with p.break_()
--> 697     output = repr(obj)
    698     for idx,output_line in enumerate(output.splitlines()):
    699         if idx:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\base.py in __repr__(self)
     76         Yields Bytestring in Py2, Unicode String in py3.
     77         """
---> 78         return str(self)
     79 
     80 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\base.py in __str__(self)
     55 
     56         if compat.PY3:
---> 57             return self.__unicode__()
     58         return self.__bytes__()
     59 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in __unicode__(self)
    632             width = None
    633         self.to_string(buf=buf, max_rows=max_rows, max_cols=max_cols,
--> 634                        line_width=width, show_dimensions=show_dimensions)
    635 
    636         return buf.getvalue()

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in to_string(self, buf, columns, col_space, header, index, na_rep, formatters, float_format, sparsify, index_names, justify, max_rows, max_cols, show_dimensions, decimal, line_width)
    719                                            decimal=decimal,
    720                                            line_width=line_width)
--> 721         formatter.to_string()
    722 
    723         if buf is None:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\formats\format.py in to_string(self)
    596         else:
    597 
--> 598             strcols = self._to_str_columns()
    599             if self.line_width is None:  # no need to wrap around just print
    600                 # the whole frame

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\formats\format.py in _to_str_columns(self)
    527                 str_columns = [[label] for label in self.header]
    528             else:
--> 529                 str_columns = self._get_formatted_column_labels(frame)
    530 
    531             stringified = []

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\formats\format.py in _get_formatted_column_labels(self, frame)
    770                             need_leadsp[x] else x]
    771                            for i, (col, x) in enumerate(zip(columns,
--> 772                                                             fmt_columns))]
    773 
    774         if self.show_row_idx_names:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\formats\format.py in <listcomp>(.0)
    769             str_columns = [[' ' + x if not self._get_formatter(i) and
    770                             need_leadsp[x] else x]
--> 771                            for i, (col, x) in enumerate(zip(columns,
    772                                                             fmt_columns))]
    773 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\formats\format.py in _get_formatter(self, i)
    363             if is_integer(i) and i not in self.columns:
    364                 i = self.columns[i]
--> 365             return self.formatters.get(i, None)
    366 
    367 

TypeError: unhashable type: 'list'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in _repr_html_(self)
    672 
    673             return self.to_html(max_rows=max_rows, max_cols=max_cols,
--> 674                                 show_dimensions=show_dimensions, notebook=True)
    675         else:
    676             return None

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in to_html(self, buf, columns, col_space, header, index, na_rep, formatters, float_format, sparsify, index_names, justify, max_rows, max_cols, show_dimensions, decimal, bold_rows, classes, escape, notebook, border, table_id, render_links)
   2263                                            render_links=render_links)
   2264         # TODO: a generic formatter wld b in DataFrameFormatter
-> 2265         formatter.to_html(classes=classes, notebook=notebook, border=border)
   2266 
   2267         if buf is None:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\formats\format.py in to_html(self, classes, notebook, border)
    727         from pandas.io.formats.html import HTMLFormatter, NotebookFormatter
    728         Klass = NotebookFormatter if notebook else HTMLFormatter
--> 729         html = Klass(self, classes=classes, border=border).render()
    730         if hasattr(self.buf, 'write'):
    731             buffer_put_lines(self.buf, html)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\formats\html.py in render(self)
    527         self.write('<div>')
    528         self.write_style()
--> 529         super(NotebookFormatter, self).render()
    530         self.write('</div>')
    531         return self.elements

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\formats\html.py in render(self)
    144 
    145     def render(self):
--> 146         self._write_table()
    147 
    148         if self.should_show_dimensions:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\formats\html.py in _write_table(self, indent)
    180             self._write_header(indent + self.indent_delta)
    181 
--> 182         self._write_body(indent + self.indent_delta)
    183 
    184         self.write('</table>', indent)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\formats\html.py in _write_body(self, indent)
    323     def _write_body(self, indent):
    324         self.write('<tbody>', indent)
--> 325         fmt_values = {i: self.fmt._format_col(i) for i in range(self.ncols)}
    326 
    327         # write values

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\formats\html.py in <dictcomp>(.0)
    323     def _write_body(self, indent):
    324         self.write('<tbody>', indent)
--> 325         fmt_values = {i: self.fmt._format_col(i) for i in range(self.ncols)}
    326 
    327         # write values

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\formats\format.py in _format_col(self, i)
    702     def _format_col(self, i):
    703         frame = self.tr_frame
--> 704         formatter = self._get_formatter(i)
    705         values_to_format = frame.iloc[:, i]._formatting_values()
    706         return format_array(values_to_format, formatter,

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\formats\format.py in _get_formatter(self, i)
    363             if is_integer(i) and i not in self.columns:
    364                 i = self.columns[i]
--> 365             return self.formatters.get(i, None)
    366 
    367 

TypeError: unhashable type: 'list'

标签: pythonpandasdataframedictionary

解决方案


是的,这是一个简单的解决方案。如果你想这样做并且不知道如何(可能没有多少人在那里......)那么你想使用一个系列而不是一个键=索引和值=列的数据框。

dict_plate = pd.Series(df_plate['condition'].values,index=df_plate['sample']).to_dict()


推荐阅读