简体   繁体   中英

How to turn a nested list without a dictionary into a pandas Dataframe?

I have created a nested list from a larger nested dictionary, and now want to convert that list into a data frame. the list i have created has no keys or values.

I have tried to convert the list into a dictionary using dict() but this does not work. the list is in this format (names and data changed for anonymity)

['Bigclient', ['All Web Site Data', '129374116'], 'Otherclient', ['All Web Site Data', '164548948'], ['Filtered website data', '142386573'], ['Test', '72551604'].

so i have a parent value 'Bigclient' that then has a child list including the name of the data and an ID number corresponding to that name. Each parent value has different amounts of child pairs. I want to make a data frame that has trhee columns like so

Client_name  dataname  ID
BigClient    All Web   129374116
Other Client All web   164548948
Other Client Filtered  142386573
Other Client Test      7255160

so the clients name (parent value) is used to group the datanames and id's

new =[]
for item in data['items']:
    name = item.get('name')
    if name:
        new.append(name)
        webprop = item.get('webProperties')
        if webprop:
            for profile in webprop:
                profile = profile.get('profiles')
                if profile:
                    for idname in profile:
                        idname = idname.get('name')
                    for idname1 in profile:
                        idname1 = idname1.get('id')
                    if idname:
                        result = [idname, idname1]
                    new.append(result)
                else:
                    continue
        else:
            continue

this is how ive built my list up, however it has no dictionaries.

Here you go:

import pandas as pd

raw_data = ['Bigclient', ['All Web Site Data', '129374116'], 'Otherclient', ['All Web Site Data', '164548948'], ['Filtered website data', '142386573'], ['Test', '72551604']]

# collect dsata
keys_list = []
values_list = [[] for _ in range(2)]
count = -1
for item in raw_data:
    if isinstance(item, str):
        keys_list.append(item)
        count += 1
    else:
        values_list[count].append(item)

# create data dictionary
data_dict = dict(zip(keys_list, values_list))

# create data frame
raw_df = pd.DataFrame(columns=['Client_name', 'data'])
for key, values in data_dict.items():
    for value in values:
        raw_df = raw_df.append({'Client_name': key, 'data': value}, ignore_index=True)

# split list data into 2 columns
spilt_data = pd.DataFrame(raw_df['data'].values.tolist(), columns=['dataname','ID'])
# concat data
result = pd.concat([raw_df, spilt_data], axis=1, sort=False)
# drop used column
result = result.drop(['data'], axis=1)

Output:

   Client_name               dataname         ID
0    Bigclient      All Web Site Data  129374116
1  Otherclient      All Web Site Data  164548948
2  Otherclient  Filtered website data  142386573
3  Otherclient                   Test   72551604

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