简体   繁体   中英

Creating mulitple dataframes from Dictionary full of list of tuples

df = {'(5, 1.01, 0.98)': [(Timestamp('2017-08-24 22:35:00'), 
    -201.99999999999915, 19798.0), (Timestamp('2017-08-25 12:10:00'),
    -199.95980000000057, 19598.0402), (Timestamp('2017-08-26 01:35:00'),
    90.00690944516465, 19688.047109445164)], '(6, 1.04, 0.99)': [(Timestamp('2017-
    08-25 16:35:00'), -808.0000000000001, 19192.0), (Timestamp('2017-08-26 
    01:35:00'), 73.45175794973963, 19265.45175794974), (Timestamp('2017-08-26 
    03:55:00'), 85.84402466986793, 19351.295782619607), (Timestamp('2017-08-26 
    11:10:00')]}

I have two keys, and multiples values that are a list of tuple for each values. How do I create a dataframe containing 3 columns relative to the tuples containing 3 element? Is it possibile? And Can I create a different dataframe for every key?

Kind Regards

If you are hoping for the following

             0                    1           2             3
0  (5, 1.01, 0.98)  2017-08-24 22:35:00 -202.000000  19798.000000
1  (5, 1.01, 0.98)  2017-08-25 12:10:00 -199.959800  19598.040200
2  (5, 1.01, 0.98)  2017-08-26 01:35:00   90.006909  19688.047109

as an output for

{'(5, 1.01, 0.98)': [(Timestamp('2017-08-24 22:35:00'), 
-201.99999999999915, 19798.0), (Timestamp('2017-08-25 12:10:00'),
-199.95980000000057, 19598.0402), (Timestamp('2017-08-26 01:35:00'),
90.00690944516465, 19688.047109445164)]}

then you can use

new_df = pd.DataFrame(data=[[k] + list(t) for k, v in df.items() for t in v])
new_df.columns = [to, whatever, you, want]

however, if you want to split out the tuple key (first column) then you can modify the above to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM