简体   繁体   English

如何将字典键转换为行并将值转换为列

[英]How to convert dictionary key as row and values as columns

How to convert nested dictionary in to data frame 如何将嵌套字典转换为数据框

My dict is below 我的字典在下面

out =  {'1.2.2.2': {'DELETE': 1,
                'GET': 5,
                'POST': 1,
                'PUT': 3},
 '2.2.2.2': {'DELETE': 1,
                   'GET': 6,
                   'POST': 3,
                   'PUT': 3},
 '3.3.3.3': {'DELETE': 0,
               'GET': 6,
               'POST': 2,
               'PUT': 1}

I want to convert in to dataframe with column values IP, DELETE, DELETE, POST, PUT 我想转换为具有IP,DELETE,DELETE,POST,PUT列值的数据框

IP key is not in my out IP密钥不在我的外面

import dataframe
pd.DataFrame([out])
df.columns =['IP', 'DELETE', 'POST', 'PUT']

You can do it like this: 您可以这样做:

df = pd.DataFrame(columns=['DELETE', 'POST', 'PUT'])
for item in out:
    list_dict = []
    list_dict.append({'DELETE':out[item]['DELETE'], 'POST':out[item]['POST'], 'PUT':out[item]['PUT']})
    df= df.append(list_dict)

and add a column named ID later. 稍后添加一个名为ID的列。 Like this: 像这样:

df['ID']=[0]*len(df)

Here I made all the ids 0. You can change it according to your data. 在这里,我将所有ID设为0。您可以根据自己的数据进行更改。

out =  {'1.2.2.2': {'DELETE': 1,
                'GET': 5,
                'POST': 1,
                'PUT': 3},
 '2.2.2.2': {'DELETE': 1,
                   'GET': 6,
                   'POST': 3,
                   'PUT': 3},
 '3.3.3.3': {'DELETE': 0,
               'GET': 6,
               'POST': 2,
               'PUT': 1}}

Create the list of columns that you want included 创建要包含的列的列表

cols = ['IP', 'DELETE', 'POST', 'PUT']

The following will transpose the dataframe, reset the index, and rename the previous index as 'IP' 以下将转置数据帧,重置索引,并将先前的索引重命名为“ IP”

pd.DataFrame(out).T.reset_index().rename(columns={'index':'IP'})[cols]

the output of this will be: 其输出将是:

    IP  DELETE  POST    PUT
0   1.2.2.2 1   1   3
1   2.2.2.2 1   3   3
2   3.3.3.3 0   2   1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将两列值转换为键值对字典? - How to convert two columns values into a key-value pair dictionary? 如何将字典转换为 pandas dataframe,键和值位于两个单独的列中? - How to convert a dictionary into a pandas dataframe with key and values in two separate columns? 如何将两个熊猫列转换为字典,但将同一第一列(键)的所有值合并为一个键? - How to convert two pandas columns into a dictionary, but merge all values of same first column (key) into one key? 如何将特定字典键的值转换为大写? - How to convert values of a specific dictionary key to uppercase? 如何将行唯一值转换为列 - How to Convert the row unique values in to columns 如何在熊猫中将行值转换为属性(列) - How to convert row values to attributes (columns) in pandas 我有数据框。 我需要创建一个以行为键的字典,以“True”作为字典值的列 - I have dataframe. I need to create a dictionary with row as the key and columns which has 'True' as the values of the dictionary 如何将未标记的键值对字典数据转换为 Python 中的 2 列 - How to convert an unlabled key value pair dictionary data into 2 columns in Python 转换字典中的整数键值 - convert integer key values in a dictionary 将dataframe列转换为dict并将其保存为json文件,列名作为dict键,行值作为dict值 - Convert dataframe columns into dict and save it as json file, with columns name as dict key and row values as dict values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM