2017-05-18 10 views
1

私はこのようなデータフレームを持っている:パンダGROUPBY 2つのsimmilar列と二つの異なる

ID | Name | Thing | belongs | match 
    ---+------+-------+---------+----- 
    1 John  10  1,2,3  9 
    2 John  10  2,4  8 

出力は次のようになります。

John 10 1,2,3,2,4 9,9,9,8,8 

どのように私まとめることができ?

答えて

1
def f(df): 
    lol = df.belongs.str.split(',').tolist() 
    lens = [len(lst) for lst in lol] 
    belongs = ','.join(map(str, np.concatenate(lol))) 
    match = ','.join(map(str, df.match.repeat(lens).tolist())) 

    return pd.Series(dict(
      belongs=belongs, 
      match=match 
     )) 

df.groupby(['Name', 'Thing']).apply(f).reset_index() 

    Name Thing belongs  match 
0 John  10 1,2,3,2,4 9,9,9,8,8 

Aわずかに異なるアプローチ。差異を特定することは、読者にとって残された課題です。

def f(df): 
    lens = df.belongs.str.count(',') + 1 
    belongs = df.belongs.str.cat(sep=',') 
    match = df.match.repeat(lens).map(str).str.cat(sep=',') 

    return pd.Series(dict(
      belongs=belongs, 
      match=match 
     )) 

print(df.groupby(['Name', 'Thing']).apply(f).reset_index()) 

    Name Thing belongs  match 
0 John  10 1,2,3,2,4 9,9,9,8,8 
+0

私が「一致」の文字列にエラーを取得しています - (「INT32」)をDTYPEするDTYPE(「Int64の」)からの配列データをキャストすることはできませんルールに従って「安全」)、それは何らかの形で解決することができますか? – SystemFailure

関連する問題