3
팬더를 사용하여 데이터 프레임으로 변환 한 CSV 파일이 있습니다. 나는 다음과 같다 사전의 형태로 출력을 필요파이썬에서 데이터 프레임을 사전으로 변환
Customer ProductID Count
John 1 50
John 2 45
Mary 1 75
Mary 2 10
Mary 5 15
: 여기에 dataframe의
{ProductID:1, Count:{John:50, Mary:75}},
{ProductID:2, Count:{John:45, Mary:10}},
{ProductID:5, Count:{John:0, Mary:15}}
나는 다음과 같은 답변 읽어
python pandas dataframe to dictionary 및
을 Convert dataframe to dictionary
이 나는 데 코드입니다 :
df = pd.read_csv('customer.csv')
dict1 = df.set_index('Customer').T.to_dict('dict')
dict2 = df.to_dict(orient='records')
을이 내 전류 출력입니다 :
dict1 = {'John': {'Count': 45, 'ProductID': 2}, 'Mary': {'Count': 15, 'ProductID': 5}}
dict2 = [{'Count': 50, 'Customer': 'John', 'ProductID': 1},
{'Count': 45, 'Customer': 'John', 'ProductID': 2},
{'Count': 75, 'Customer': 'Mary', 'ProductID': 1},
{'Count': 10, 'Customer': 'Mary', 'ProductID': 2},
{'Count': 15, 'Customer': 'Mary', 'ProductID': 5}]