Используя Pandas, вы можете читать данные в кадре данных Pandas следующим образом:
import pandas as pd
df = pd.read_csv('data.csv')
В этот момент параметр значения dataframes содержит данные таблицы. Вы можете перебирать эти данные для извлечения и создания словаря, который вы ищете. Что-то вроде:
patient_info_dict = {}
for row in df.values:
# At this point, the first value in 'row' is your dictionary key.
# Check if the patient id is already a key in the dictionary
if row[0] not in patient_info_dict:
# Initialize an empty array
patient_info_dict[row[0]] = []
# Append the remaining data except for the key and the last value
patient_info_dict[row[0]].append(row[1:-1])
# If the patient id is already a key in the dictionary:
else:
# Append the remaining data except for the key and the last value
patient_info_dict[row[0]].append(row[1:-1])
Если вы печатаете словарь с:
print(patient_info_dict)
Вы получите следующий вывод:
{'A12kxk': [array([23, 45, 10, 20], dtype=object)], 'Aldkd2': [array([92, 22, 12, 39], dtype=object), array([29, 11, 98, 34], dtype=object)]}
Другой ответ определенно более питоничен и, вероятно, более эффективен. Однако, если вы новичок в Python / Pandas, это может быть полезно для понимания того, что именно происходит.