resolver101
resolver101

Reputation: 2255

pandas: saving data frame to dict in correct time format

I want the following output from my pandas dataframe df:

{1622564509268542720: '36.15', 1622564509311439360: '37.83', 1622564509312406784: '38.20', 1622564509357944832: '40.40', 1622564509358921984: '33.46', 1622564509404489472: '38.37', 1622564509405471232: '37.15'}

When i type df.head(3).to_dict(), it outputs in the following format

{'ApparentPower': {Timestamp('2021-06-01 16:21:50.754080768'): 40.83, Timestamp('2021-06-01 16:21:50.755921664'): 106.41, Timestamp('2021-06-01 16:21:50.800695808'): 46.56}}

Whats the easiest way to get it into the format i need above?

Upvotes: 0

Views: 74

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

Try converting the index to integer:

df.set_index(df.index.astype(int))['ApparentPower'].to_dict()

Output:

{1622564510754080768: 40.83,
 1622564510755921664: 106.41,
 1622564510800695808: 46.56}

Upvotes: 3

Related Questions