Считайте столбец Excel в dict, посчитайте, как часто встречается ген, и распечатайте его на новом листе Excel. - PullRequest
0 голосов
/ 13 марта 2019

Я не нашел решения своей проблемы.Что мне нужно сделать, так это прочитать один столбец Excel, посчитать, сколько раз встречается значение, удалить все дубликаты, а затем, поскольку файл Excel очень большой, записать это в новый лист.Я использовал этот код.

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
from collections import Counter, defaultdict

df = pd.read_excel('file.xlsx', sheet_name='p val FDR <0.0001 genes')

g = df['Gene_name']

count = pd.DataFrame(g).set_index()[1].apply(pd.Series.value_counts).stack()

count.to_dict()

writer = ExcelWriter('file.xlsx')
df.to_excel(writer,'Tabelle1', index=False)
writer.save()

и получаю следующие сообщения об ошибках:

Traceback (most recent call last):
File "C:\Users\schwa\lpthw\venv\lib\site-packages\pandas\core\indexes\base.py", line 2656, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:/Users/schwa/lpthw/file.py", line 14, in <module>
count = pd.DataFrame(g).set_index(0)[1].apply(pd.Series.value_counts).stack()
File "C:\Users\schwa\lpthw\venv\lib\site-packages\pandas\core\frame.py", line 4178, in set_index
level = frame[col]._values
File "C:\Users\schwa\lpthw\venv\lib\site-packages\pandas\core\frame.py", line 2927, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\schwa\lpthw\venv\lib\site-packages\pandas\core\indexes\base.py", line 2658, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

Process finished with exit code 1

Я нашел подобные вопросы здесь, но ни один из них не решил мою проблему.Как мне решить эту ошибку?

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