簡體   English   中英

在數據框列的列表列表中查找列表的出現

[英]Find occurences of list in a list of list in a dataframe column

我有一個數據框 df,只有一列。

data = {'details': [['brand : honda', 'car : city', 'colour : black'],['brand : toyota', 'car : innova'],
                    ['brand : honda', 'colour : red'], ['brand : toyota', 'car : corolla', 'colour : white', 'type : sedan']]}
df = pd.DataFrame(data,columns= ['details'])
df

我想將數據框拆分為不同的列並獲得一個如下所示的數據框 -

data = {'details': [['brand : honda', 'car : city', 'colour : black'],['brand : toyota', 'car : innova'],
                    ['brand : honda', 'colour : red'], ['brand : toyota', 'car : corolla', 'colour : white', 'type : sedan']],
        'brand': ['honda', 'toyota', 'honda', 'toyota'],
        'car': ['city','innova','','corolla'],
        'colour': ['black','','red','white'],
        'type': ['','','','sedan']
        }
df2 = pd.DataFrame(data,columns= ['details', 'brand', 'car', 'colour', 'type'])
df2

我嘗試了以下但沒有奏效 -

a2 = []
b2 = []
c2 = []
d2 = []
for i in df['details']:
    for j in range(len(i)):
        if 'brand :' in i[j]:
            print 'lalala'
            a1 = i[j]
            a2.append(a1)
        else:
            a1 = ''
            a2.append(a1)
        if 'car :' in i[j]:
            print 'lalala'
            b1 = i[j]
            b2.append(b1)
        else:
            b1 = ''
            b2.append(b1)
        if 'colour :' in i[j]:
            c1 = i[j]
            c2.append(c1)
        else:
            c1 = ''
            c2.append(c1)
        if 'type :' in i[j]:
            d1 = i[j]
            d2.append(d1)
        else:
            d1 = ''
            d2.append(d1)
df['brand'] = a2
df['car'] = b2
df['colour'] = c2
df['type'] = d2

請幫忙,因為我遇到了一個主要的障礙。

假設詳細信息類型已知,您可以嘗試以下操作:

details_types = ['brand', 'car', 'colour', 'type']

for x in details_types :
    df[x] = None

for idx, value in df.iterrows(): 
    for col_details in df.iloc[idx, 0]:
        feature = col_details.replace(' ', '').split(':')[0]
        value = col_details.replace(' ', '').split(':')[1]
        df.iloc[idx, list(df.columns).index(feature)] = value

輸出

|   |                      details                      | brand  |   car   | colour | type  |
|---|---------------------------------------------------|--------|---------|--------|-------|
| 0 | [brand : honda, car : city, colour : black]       | honda  | city    | black  | None  |
| 1 | [brand : toyota, car : innova]                    | toyota | innova  | None   | None  |
| 2 | [brand : honda, colour : red]                     | honda  | None    | red    | None  |
| 3 | [brand : toyota, car : corolla, colour : white... | toyota | corolla | white  | sedan |

一個稍微簡單的方法可能是以下 -

data = {'details': [['brand : honda', 'car : city', 'colour : black'],['brand : toyota', 'car : innova'],
                    ['brand : honda', 'colour : red'], ['brand : toyota', 'car : corolla', 'colour : white', 'type : sedan']]}

#takes a string and returns a dict based on ':'
def fix(l):
    return dict(s.split(':') for s in l)

#flatten and fix the lists of lists to get a list of dicts
dicts = [fix(i) for sublist in data.values() for i in sublist]

#Add the lists into a single dataframe (optional add the 'Details' column)
df = pd.DataFrame.from_dict(dicts)
df['details'] = pd.DataFrame.from_dict(data)  #adding 'Details' col
print(df)
    brand       car  colour    type   \
0    honda      city   black     NaN   
1   toyota    innova     NaN     NaN   
2    honda       NaN     red     NaN   
3   toyota   corolla   white   sedan   

                                             details  
0        [brand : honda, car : city, colour : black]  
1                     [brand : toyota, car : innova]  
2                      [brand : honda, colour : red]  
3  [brand : toyota, car : corolla, colour : white...  
import pandas as pd
from collections import ChainMap
data = {'details': [['brand : honda', 'car : city', 'colour : black'],['brand : toyota', 'car : innova'],
                ['brand : honda', 'colour : red'], ['brand : toyota', 'car : corolla', 'colour : white', 'type : sedan']]}
#STEP_1
lists=[[{y.split(':')[0]:y.split(':')[1]} for y in x] for x in data['details']]
#STEP_2
data_df = [dict(ChainMap(*x)) for x in lists]
#STEP_3
data_df=pd.DataFrame(data_df)
#STEP_4
data_df['details']=data['details']
print(data_df)
'''Explanation:
STEP_1: It creates list of lists with dictionary elements

[[{'brand ': ' honda'}, {'car ': ' city'}, {'colour ': ' black'}],
[{'brand ': ' toyota'}, {'car ': ' innova'}],
[{'brand ': ' honda'}, {'colour ': ' red'}],
[{'brand ': ' toyota'},
{'car ': ' corolla'},
{'colour ': ' white'},
{'type ': ' sedan'}]]

STEP_2: It is to convert list of lists to list of dictionaries

[{'colour ': ' black', 'car ': ' city', 'brand ': ' honda'},
{'car ': ' innova', 'brand ': ' toyota'},
{'colour ': ' red', 'brand ': ' honda'},
{'type ': ' sedan',
'colour ': ' white',
'car ': ' corolla',
'brand ': ' toyota'}]

STEP_3: As we can directly create a dataframe from list of 
dictionaries, it creates a dataframe with 4 columns that are brand, 
car, color & type

STEP_4: Add the column 'details' using the 'data' variable'''

用:

代碼

# extract the patterns
pattern = r"(?:brand : (?P<brand>\w+))|(?:car : (?P<car>\w+))|(?:colour : (?P<colour>\w+))|(?:type : (?P<type>\w+))"
expanded = df.explode("details")["details"].str.extract(pattern)

# convert to expected format after extracting the patterns
new = expanded.groupby(level=0).first().fillna("")
print(new)

輸出

    brand      car colour   type
0   honda     city  black       
1  toyota   innova              
2   honda             red       
3  toyota  corolla  white  sedan

在您可以通過執行以下操作將所有內容連接在一起之后:

result = pd.concat([df, new], axis=1)
print(result)

輸出(滿)

                                             details   brand  ... colour   type
0        [brand : honda, car : city, colour : black]   honda  ...  black       
1                     [brand : toyota, car : innova]  toyota  ...              
2                      [brand : honda, colour : red]   honda  ...    red       
3  [brand : toyota, car : corolla, colour : white...  toyota  ...  white  sedan

[4 rows x 5 columns]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM