簡體   English   中英

熊貓-在數據框的值中具有不等長的轉置列表

[英]Pandas - transpose lists with unequal length in the value of dataframe

這個問題是對Pandas的擴展:將列中的列表分成多行 ,現在我不想合並更多的DataFrame。 而且我無法使其與超過2個dfs一起使用。

我有這個DataFrame:

  Index     Job positions   Job types   Locations
      0          [5]         [6]        [3, 4, 5]
      1          [1]         [2, 6]     [3, NaN] 
      2          [1,3]       [9, 43]    [1]

我想要數字的每個單一組合,因此最終結果將是:

index   Job position  Job type  Location
    0   5             6         3
    0   5             6         4
    0   5             6         5
    1   1             2         3
    1   1             2         NaN
    1   1             6         3
    1   1             6         NaN
    2   1             9         1
    2   1             43        1
    2   3             9         1
    2   3             43        1

所以我要做的是將列轉換為Series:

positions = df['Job positions'].apply(pd.Series).reset_index().melt(id_vars='index').dropna()[['index', 'value']].set_index('index')
types = df['Job types'].apply(pd.Series).reset_index().melt(id_vars='index').dropna()[['index', 'value']].set_index('index')
locations = df['Locations'].apply(pd.Series).reset_index().melt(id_vars='index').dropna()[['index', 'value']].set_index('index')

dfs = [positions, types, locations]

然后嘗試像這樣合並它們:

df_final = reduce(lambda left,right: pd.merge(left,right,left_index=True, right_index=True, how="left"), dfs)

但似乎是用NaN跳過了這些字段-如何防止這種情況?

1行:

import itertools

dfres = pd.DataFrame([(i[0],)+j for i in df.values for j in itertools.product(*i[1:])]
        ,columns=df.columns).set_index('index')


       Job positions  Job types  Locations
index                                     
0                  5          6        3
0                  5          6        4
0                  5          6        5
1                  1          2        3
1                  1          2        NaN
1                  1          6        3
1                  1          6        NaN
2                  1          9        1
2                  1         43        1
2                  3          9        1
2                  3         43        1

暫無
暫無

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

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