Bringing the csv and dict Python

Asked 2 years ago, Updated 2 years ago, 14 views

I'd like to bring up the csv and put it in a dictionary form. {test1:{testgroup3:{a:100, b:200, c:200}, testgroup2{a:100, b:1000, c:200}, testgroup{a:200, b:100, c:200}}, test2{testgroup3{a:200, b:100, c:200}}, testgroup2{a:200, b:100, c:200}}, testgroup{a:200, b:100, c:200}}}I want to create a dictionary format in a dictionary like this.

import pandas as pd
df = pd.read_csv('csv.csv',sep=',')
dict1 = df.set_index('campaign').to_dict('list')
dict2 = df.to_dict(orient="index")

print(dict2)

I don't know what to do anymore after this time.

The csv file is in the following format:

campaign group abc
test1 testgroup1 100 200 200

python

2022-09-20 19:51

3 Answers

I think there's a better way, but to_dict doesn't work out the way you want it to.

Please keep that in mind.

#test.csv
#campaign,group,a,b,c
#test1,testgroup1,101,201,301
#test1,testgroup2,101,201,301
#test2,testgroup1,102,202,302
#test2,testgroup2,102,202,302
#test2,testgroup3,102,202,302

import pandas as pd

def to_dict(df):
    tmp = ''
    sep1 = {}

    for idx, i in enumerate(df.iterrows()):
        if i[1][0] != tmp:
            tmp = i[1][0]
            sep2 = {}
        sep3 = i[1][2:].to_dict()
        sep2[i[1][1]] = sep3
        sep1[tmp] = sep2
    return sep1

def main():
    df = pd.read_csv('test.csv', sep=',')
    r = to_dict(df)
    print(r['test1'])
    print(r['test2'])
    print(r)

if __name__ == "__main__":
    main()

#result
#{'testgroup1': {'a': 101, 'b': 201, 'c': 301}, 'testgroup2': {'a': 101, 'b': 201, 'c': 301}}
#{'testgroup1': {'a': 102, 'b': 202, 'c': 302}, 'testgroup2': {'a': 102, 'b': 202, 'c': 302}, 'testgroup3': {'a': 102, 'b': 202, 'c': 302}}
#{'test1': {'testgroup1': {'a': 101, 'b': 201, 'c': 301}, 'testgroup2': {'a': 101, 'b': 201, 'c': 301}}, 'test2': {'testgroup1': {'a': 102, 'b': 202, 'c': 302}, 'testgroup2': {'a': 102, 'b': 202, 'c': 302}, 'testgroup3': {'a': 102, 'b': 202, 'c': 302}}}


2022-09-20 19:51

There are many interesting options in pd.DataFrame's to_dict.

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_dict.html

>>> from io import StringIO
>>> import pandas as pd
>>> s = '''campaign group abc
test1 testgroup1 100 200 200
test1 testgroup2 200 300 400
test2 testgroup3 300 10 20'''
>>> df = pd.read_csv(StringIO(s.replace(' ', ',')))
>>> df
  Campaign Group a bc
0    test1  testgroup1  100  200  200
1    test1  testgroup2  200  300  400
2    test2  testgroup3  300   10   20
>>> dic = {}
>>> for camp, g in df.groupby('campaign'):
    df2 = g.iloc[:,1:].set_index ("Group")
    dic[camp] = df2.to_dict('index')


>>> dic
{'test1': {'testgroup1': {'a': 100, 'b': 200, 'c': 200}, 'testgroup2': {'a': 200, 'b': 300, 'c': 400}}, 'test2': {'testgroup3': {'a': 300, 'b': 10, 'c': 20}}}
>>> from pprint import pprint
>>> pprint(dic)
{'test1': {'testgroup1': {'a': 100, 'b': 200, 'c': 200},
           'testgroup2': {'a': 200, 'b': 300, 'c': 400}},
 'test2': {'testgroup3': {'a': 300, 'b': 10, 'c': 20}}}


2022-09-20 19:51

There's also this way.

from io import StringIO
s = StringIO('''campaign,group,a,b,c
test1,testgroup1,101,201,301
test1,testgroup2,101,201,301
test2,testgroup1,102,202,302
test2,testgroup2,102,202,302
test2,testgroup3,102,202,302
''')

df = pd.read_csv(s, sep=",")
df.set_index(['campaign', 'group'], inplace=True)
df.groupby(level=0).apply(lambda _df: _df.xs(_df.name).to_dict('index')).to_dict()

{'test1': {'testgroup1': {'a': 101, 'b': 201, 'c': 301},
  'testgroup2': {'a': 101, 'b': 201, 'c': 301}},
 'test2': {'testgroup1': {'a': 102, 'b': 202, 'c': 302},
  'testgroup2': {'a': 102, 'b': 202, 'c': 302},
  'testgroup3': {'a': 102, 'b': 202, 'c': 302}}}


2022-09-20 19:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.