簡體   English   中英

根據另一列中的文本在 pandas 中創建列

[英]Create columns in pandas based on text from another column

我在 python 中有一個 pandas Dataframe 有一個名為“描述”的列,其中包含一組由“\n”分隔的文本元素。 我想通過拆分文本元素在同一 Dataframe 中創建新列。 例如,我有:

 Description

 '\nA: Elephant\nB: Cats\nC:Dog'

我想獲得具有相關元素的相應列數,如下所示:

Description                         A         B    C

 '\nA: Elephant\nB: Cats\nC:Dog'    Elephant  Cat  Dog

我該怎么做 Python 或 Python Pandas?

首先使用stripsplit創建字典列表並傳遞給DataFrame構造函數:

df = pd.DataFrame({'Description':['\nA: Elephant\nB: Cats\nC:Dog',
                                  '\nA: Monkey\nB: Duck\nC:Dog']})
    
L = [dict([y.split(':') for y in x.strip().split('\n')]) for x in df['Description']]

df = df.join(pd.DataFrame(L, index=df.index))
print (df)
                     Description          A      B    C
0  \nA: Elephant\nB: Cats\nC:Dog   Elephant   Cats  Dog
1    \nA: Monkey\nB: Duck\nC:Dog     Monkey   Duck  Dog

編輯:如果沒有:在拆分值,你可以省略這個值:

df = pd.DataFrame({'Description':['\nA: Elephant\nB: Cats\nC:Dog',
                                  '\nA: Monkey\nB: Duck\nCDog']})
    
L = [dict([y.split(':') for y in x.strip().split('\n') if ':' in y]) 
           for x in df['Description']]

df = df.join(pd.DataFrame(L, index=df.index))
print (df)
                     Description          A      B    C
0  \nA: Elephant\nB: Cats\nC:Dog   Elephant   Cats  Dog
1     \nA: Monkey\nB: Duck\nCDog     Monkey   Duck  NaN

暫無
暫無

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

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