1

Im converting DataFrame into List using following code

resultDataFrame = []
resultDataFrame = pd.DataFrame(resultDataFrame, columns=('day', 'hour', 'minute'))

and appending values ...

resultDataFrame.to_dict('l')
{'day': [6], 'hour': [12], 'minute': [54]}

But, I want to get it like

{'day': 6, 'hour': 12, 'minute': 54}

How to change the format?

1
  • How does it working? Commented Mar 13, 2018 at 9:29

1 Answer 1

2

You can select first row for Series e.g. by iloc or loc and create dict by Series.to_dict:

df = pd.DataFrame({'day': [6], 'hour': [12], 'minute': [54]})
print (df)
   day  hour  minute
0    6    12      54

print (df.iloc[0])
day        6
hour      12
minute    54
Name: 0, dtype: int64

print (df.iloc[0].to_dict())
{'day': 6, 'minute': 54, 'hour': 12}

Another possible solution is convert to records - list of dict and select by indexing:

print (df.to_dict('r'))
[{'day': 6, 'minute': 54, 'hour': 12}]

print (df.to_dict('r')[0])
{'day': 6, 'minute': 54, 'hour': 12}
Sign up to request clarification or add additional context in comments.

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.