简体   繁体   中英

How to convert pandas dataframe(series) to dict

Please help manipulate the dataframe pandas.

I have a query like this and pandas a dateframe:

    sql_query = pd.read_sql_query(query, connection)
    df = pd.DataFrame(sql_query, columns=['id', 'w_id', 'w_date_from', 'w_date_to', 'br_id'])
    df['w_date_from'] = pd.to_datetime(df['w_date_from'].dt.tz_convert('Europe/Moscow'))
    df['w_date_from'] = df['w_date_from'].dt.strftime('%H:%M')
    df['w_date_to'] = df['w_date_to'].dt.day_name()

    df = df.groupby(
        ['w_date_to', 'w_date_from']
    )['br_id', 'w_id'].agg(['nunique']).rename_axis(['weekday', 'time'])
    df['percent'] = df['br_id'] / df['w_id']
    del df['br_id']
    del df['w_id']
    print(df)

The result of this code is like this:

                   percent
                          
weekday   time            
Friday    07:40   9.666667
          09:30   7.000000
          10:30   9.750000
...                    ...
Wednesday 15:10   6.833333
          16:30  14.166667
          18:30  26.166667

[81 rows x 1 columns]

I need to make the result like this:

"weekday_time": {
        "Friday": {
            "07:40": 7.67,
            "09:30": 8.0,
            "10:30": 5.0
        },
        
        ...

        "Wednesday": {
            "10:30": 14.0,
            "11:50": 8.43,
            "13:00": 12.5
        }
    },

You can use pandas' built-in to_dict() method.

To get your multi-level index into the format you want, if there's two levels, you can first convert the series to a dataframe with unstack() .

You want the first level to be the days, and the second to be the hours, so you'll have to unstack the 0th level instead of the -1st.

The following should do the trick:

df["percent"].unstack(level=0).to_dict()

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