0

I am trying to convert dataframe to dictionary(as they are faster when filtering on key) I am currently using

t3 = time()
r={}
for i in df.index.unique():
    r[i]=[]
    r[i].append(df.loc[i].values)
print(round((time()-t3), 1), "s")

this type of conversion is slow. Is there an alternative to this? I want index of dataframe as key and row as values with multiple values on a single key

1
  • can you provide an example of it Commented Aug 22, 2020 at 7:29

2 Answers 2

1

Use pandas.DataFrame.to_dict after transposing to get index as key and row values as values:

import pandas as pd

df = pd.DataFrame({'col1': [1, 2], 'col2': ['a', 'b']})
r = df.T.to_dict('list')
print(r)

Output:

{0: [1, 'a'], 1: [2, 'b']}
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried this but the issue with same is that it returns single value for a key even though there are multiple values for a key
0

I was able to convert my dataframe with multiple duplicate indexes to dictionary using:

dicti={}
for line in df.itertuples():
   if line.index not in dicti:
      dicti[line.index]=[]
      dicti[line.index].append(list(line))
   else:
      dicti[line.index].append(list(line))

With 5 sec run time for 600k rows

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.