1

Given a Pandas df:

        Name      V1        V2
        a         1         2
        a         3         4
        a         5         6
        b         7         8
        b         9         10
        c         11        12
        ...

How to reform it into a complex dictionary of format:

        {a: [(1,2), (3,4), (5,6)], b: [(7,8), (9,10)], c: [(11,12)], ...}

Please note that values of the same name also needs to be combined across rows; like "a" has three rows to be combined as one signel value array of number pairs.

zhihao_li
  • 183
  • 10
  • Does this answer your question? [Convert a Pandas DataFrame to a dictionary](https://stackoverflow.com/questions/26716616/convert-a-pandas-dataframe-to-a-dictionary) – jlewkovich Jan 14 '20 at 03:15
  • I don't think so, because values are simply combined across columns in that post while they also need to be combined across rows here in this post – zhihao_li Jan 14 '20 at 03:24

4 Answers4

1

If you don't mind the results being list instead of tuple, you can also use groupby in a dict comprehension:

d = {group:items[["V1","V2"]].values.tolist() for group, items in df.groupby("Name")}

print (d)

{'a': [[1, 2], [3, 4], [5, 6]], 'b': [[7, 8], [9, 10]], 'c': [[11, 12]]}
Henry Yik
  • 22,275
  • 4
  • 18
  • 40
1

Try:

df['tup'] = df[['V1','V2']].agg(tuple, axis=1)
df.groupby('Name')['tup'].agg(list).to_dict()

Output:

{'a': [(1, 2), (3, 4), (5, 6)], 'b': [(7, 8), (9, 10)], 'c': [(11, 12)]}
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
0

Assuming the DataFrame variable be data_frame

print(data_frame)
Name      V1        V2
a         1         2
a         3         4
a         5         6
b         7         8
b         9         10
c         11        12
data_dict = {}

for data in data_frame.values:
    print(data)
    data_dict[data[0]] = [j for j in data[1:]]

print(data_dict)

Also, there are some methods on the data frame object like to_dict() variants. You can use to_dict('records') also and manipulate accordingly.

Referece: Data Frame Dict

Harshit Garg
  • 2,137
  • 21
  • 23
0

Check this out, specific to columns

data_frame = {
    "Name": ["a", "a", "a", "b", "b", "c"],
    "V1": [1, 3, 5, 7, 9, 11],
    "V2": [2, 4, 6, 8, 10, 12]
}
df = pd.DataFrame(data_frame, columns=['Name', 'V1', 'V2'])

data_dict = {}
for i, row in df.iterrows():
    data_dict[row["Name"]] = [row['V1'], row['V2']]

print(data_dict)

Output be like

{'a': [5, 6], 'b': [9, 10], 'c': [11, 12]}
Arun Augustine
  • 1,690
  • 1
  • 13
  • 20