簡體   English   中英

在Python中將一個字符串列拆分為多個列

[英]split one string column to multiple columns in Python

我有以下數據幀:

df = pd.DataFrame({'scene':[{"living":"0.515","kitchen":"0.297"}, {"kitchen":"0.401","study":"0.005"}, {"study":"0.913"}, {}, {"others":"0"}], 'id':[1, 2, 3 ,4, 5]}) 

id        scene
01      {"living":"0.515","kitchen":"0.297"}
02      {"kitchen":"0.401","study":"0.005"}
03      {"study":"0.913"}
04      {}
05      {"others":"0"}

我想創建一個新的數據幀,如下所示,有人可以幫我用Pandas創建嗎?

id      living     kitchen     study     others
01      0.515       0.297        0         0 
02        0         0.401      0.005       0
03        0           0        0.913       0
04        0           0          0         0 
05        0           0          0         0

簡單的解決方案是將scene列轉換為字典列表,並使用默認構造函數創建新數據框:

pd.DataFrame(df.scene.tolist()).fillna(0)

結果:

  kitchen living others  study
0   0.297  0.515      0      0
1   0.401      0      0  0.005
2       0      0      0  0.913
3       0      0      0      0
4       0      0      0      0

創建DataFrame的“默認”方法之一是使用字典列表。 在這種情況下,列表的每個字典將被轉換為單獨的行,並且dict的每個鍵將用於列標題。

關於你的數據,

df = pd.DataFrame({'scene':[{"living":"0.515","kitchen":"0.297"}, {"kitchen":"0.401","study":"0.005"}, 
                        {"study":"0.913"}, {}, {"others":"0"}], 
               'id':[1, 2, 3 ,4,5], 's': ['a','b','c','d','e']})

df:
    id  s   scene
0   1   a   {'kitchen': '0.297', 'living': '0.515'}
1   2   b   {'kitchen': '0.401', 'study': '0.005'}
2   3   c   {'study': '0.913'}
3   4   d   {}
4   5   e   {'others': '0'}

有兩種方法可以做到這一點,

  1. 在一行中,您必須將除“scene”之外的所有列名稱輸入到set_index函數

     df = df.set_index(['id', 's'])['scene'].apply(pd.Series).fillna(0).reset_index() 

    這將輸出:

      id s kitchen living study others 0 1 a 0.297 0.515 0 0 1 2 b 0.401 0 0.005 0 2 3 c 0 0 0.913 0 3 4 d 0 0 0 0 4 5 e 0 0 0 0 
  2. 在兩行中,您可以在其中創建例外結果並將其連接到原始數據框。

     df1 = df.scene.apply(pd.Series).fillna(0) df = pd.concat([df, df1], axis=1) 

    這使,

      id s scene kitchen living study others 0 1 a {'kitchen': '0.297', 'living': '0.515'} 0.297 0.515 0 0 1 2 b {'kitchen': '0.401', 'study': '0.005'} 0.401 0 0.005 0 2 3 c {'study': '0.913'} 0 0 0.913 0 3 4 d {} 0 0 0 0 4 5 e {'others': '0'} 0 0 0 0 

更新。 這一個很完美。 歡迎提出您的建議,使其更簡潔。

import json
import pandas as pd

df = pd.DataFrame({'scene':[{"living":"0.515","kitchen":"0.297"}, {"kitchen":"0.401","study":"0.005"}, {"study":"0.913"}, {}, {"others":"0"}], 'id':[1, 2, 3 ,4,5], 's':['a','b','c','d','e']}) 
def test(Scene, type):
    Scene = json.loads(Scene)
    if type in Scene.keys():
        return Scene[type]
    else:
        return ""

a = ['living', 'kitchen', 'study', 'others']
for b in a:
    df[b] = df['Scene'].map(lambda Scene: test(Scene, b.lower()))

cols = ['living', 'kitchen', 'study', 'others']
df[cols] = df[cols].replace({'': 0})
df[cols] = df[cols].apply(pd.to_numeric, errors='coerce', axis=1)

完美的一線解決方案就在這里,感謝所有幫助:

df.join(df['scene'].apply(json.loads).apply(pd.Series))

暫無
暫無

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

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