Как преобразовать 2-колоночный фрейм данных в словарь, не превращая ключи в список - PullRequest
0 голосов
/ 23 апреля 2020

Я предвосхищу это, говоря, что я очень любитель, и хотя я тщательно исследовал свою проблему, я не нашел решения. Я предполагаю, что решение простое, но мы увидим.

Упрощенно, у меня есть фрейм данных с именами столбцов A, B, C, D и т. Д. c., И я хочу изменить эти имена к a, b, c, d, et c .. Список имен столбцов длинный, поэтому для достижения этого я импортировал фрейм данных из файла Excel с 2 столбцами (я использовал здесь 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'...} Держу пари, что он будет работать нормально.

Итак, в общем, как мне перевернуть мой фрейм данных в словарь без списков, который отформатирован как таковой? Или это невозможно, и я должен подойти к этому по-другому?

Спасибо!

Вот мой фактический код:

INPUT

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'

1 Ответ

0 голосов
/ 23 апреля 2020

Да, это было простое решение. Если вы хотите сделать это и не знаете, как это сделать (вероятно, не многие из вас ...), тогда вы хотите использовать ряд, а не фрейм данных с ключами = индекс и значения = столбец.

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...