2017-06-26 10 views
3

パンダのDataframeの列の部分を比較することは可能ですか?私は以下のデータフレームの例を持っています。そこには4つの言語が保存されています(en、de、nl、ua)、各言語は同じキー/同じ量のキーを持っていますが、私は静的な列を持っているので、完了するために値は常に同じままです)。パンダのデータフレームの集約部分を比較するには?

│ Lang │ Static │ # Missing │ Keys   │ 
│ de │ x  │ 0   │    │ 
│ nl │ x  │ 1   │ key_3   │ 
│ ua │ x  │ 2   │ key_2, key_3 │ 

:私は英語の1(「en」とここ)と比較して、言語ごとに不足しているので、このようなものは、所望の出力がどうなるかのキーとどのように多くのを確認する必要があり

static │ langs │ keys │ values 

x  │ en  │ key_1 │ value_en_1 
x  │ en  │ key_2 │ value_en_2 
x  │ en  │ key_3 │ value_en_3 
x  │ de  │ key_1 │ value_de_1 
x  │ de  │ key_2 │ value_de_2 
x  │ de  │ key_3 │ value_de_3 
x  │ nl  │ key_1 │ value_nl_1 
x  │ nl  │ key_2 │ value_nl_2 
x  │ ua  │ key_1 │ value_ua_1 

    keys values 
       count count 
static language    
x  de   3  3 
     en   3  3 
     nl   2  2 
     ua   1  1 
01:これはdf_summの出力がある

import pandas as pd 

# this is read from a csv, but I'll leave it as list of lists for simplicity 
rows = [ 
    ['x', 'en', 'key_1', 'value_en_1'], 
    ['x', 'en', 'key_2', 'value_en_2'], 
    ['x', 'en', 'key_3', 'value_en_3'], 
    ['x', 'de', 'key_1', 'value_de_1'], 
    ['x', 'de', 'key_2', 'value_de_2'], 
    ['x', 'de', 'key_3', 'value_de_3'], 
    ['x', 'nl', 'key_1', 'value_nl_1'], 
    ['x', 'nl', 'key_2', 'value_nl_2'], 
    ['x', 'ua', 'key_1', 'value_en_1'] 
] 

# create DataFrame out of rows of data 
df = pd.DataFrame(rows, columns=["static", "language", "keys", "values"]) 
# print out DataFrame 
print("Dataframe: ", df) 

# first group by language and the static column 
df_grp = df.groupby(["static", "language"]) 

# try to sum the number of keys and values per each language 
df_summ = df_grp.agg(["count"]) 

# print out the sums 
print() 
print(df_summ) 

# how to compare? 
# how to get the keys? 

:これは私の現在の進行状況です

この時点ではどのように進めるのか分かりません。私はどんなヘルプ/ヒントにも感謝しています。

P.S.これはPython 3.5にあります。

答えて

3

EDIT:

#get set per groups by static and language 
a = df.groupby(["static",'language'])['keys'].apply(set).reset_index() 
#filter only en language per group by static and create set 
b = df[df['language'] == 'en'].groupby("static")['keys'].apply(set) 
#subtract mapped set b and join 
c = (a['static'].map(b) - a['keys']).str.join(', ').rename('Keys') 
#substract lengths 
m = (a['static'].map(b).str.len() - a['keys'].str.len()).rename('Missing') 

df = pd.concat([a[['static','language']], m, c], axis=1) 
print (df) 
    static language Missing   Keys 
0  x  de  0    
1  x  en  0    
2  x  nl  1   key_3 
3  x  ua  2 key_3, key_2 

EDIT:

私は変更データを試してみてください。

rows = [ 
    ['x', 'en', 'key_1', 'value_en_1'], 
    ['x', 'en', 'key_2', 'value_en_2'], 
    ['x', 'en', 'key_3', 'value_en_3'], 
    ['x', 'de', 'key_1', 'value_de_1'], 
    ['x', 'de', 'key_2', 'value_de_2'], 
    ['x', 'de', 'key_3', 'value_de_3'], 
    ['x', 'nl', 'key_1', 'value_nl_1'], 
    ['x', 'nl', 'key_2', 'value_nl_2'], 
    ['x', 'ua', 'key_1', 'value_en_1'], 
    ['y', 'en', 'key_1', 'value_en_1'], 
    ['y', 'en', 'key_2', 'value_en_2'], 
    ['y', 'de', 'key_4', 'value_en_3'], 
    ['y', 'de', 'key_1', 'value_de_1'], 
    ['y', 'de', 'key_2', 'value_de_2'], 
    ['y', 'de', 'key_3', 'value_de_3'], 
    ['y', 'de', 'key_5', 'value_nl_1'], 
    ['y', 'nl', 'key_2', 'value_nl_2'], 
    ['y', 'ua', 'key_1', 'value_en_1'] 
] 

# create DataFrame out of rows of data 
df = pd.DataFrame(rows, columns=["static", "language", "keys", "values"]) 
# print out DataFrame 
#print(df) 

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

print (df) 
    static language Missing   Keys 
0  x  de  0    
1  x  en  0    
2  x  nl  1   key_3 
3  x  ua  2 key_3, key_2 
4  y  de  -3    
5  y  en  0    
6  y  nl  1   key_1 
7  y  ua  1   key_2 

問題ためdeためのものです0静的にen言語のように多くのキーがあります。

+0

、ありがとうございました。 2番目の解決策はそれより複雑であるようですが、最初のものよりも好む理由がありますか? –

+0

私は大規模なデータでは、いくつかの欠落しているカテゴリだけで速くなると思います。 – jezrael

+0

ありがとう、別の質問です。他の言語と英語の言語との比較はどこですか?あなたの解決策では、ドイツ語に英語よりも多くの鍵があるとすれば、英語の鍵は1つの鍵がないと表示されるでしょうか? –

1

最初に不足している列をグループ化し、nansの数を数えて作成することができます。次に、キー列を作成し、静的列を追加します。

df2 = (
    df.groupby('langs')['keys'].apply(lambda x: x.values) 
     .apply(pd.Series) 
     .assign(Missing=lambda x: x.isnull().sum(axis=1)) 
) 

(
    df2[['Missing']].assign(static=df.static.iloc[0], 
          Keys=df2.apply(lambda x: ','.join(df2.loc['en'].loc[x.isnull()]),axis=1))  
) 

Out[44]: 
     Missing   Keys static 
langs        
de   0     x 
en   0     x 
nl   1  key_3  x 
ua   2 key_2,key_3  x 
1
# First we group with `language` and aggregate `static` with `min` (it's always the same anyway) 
# and `keys` with a lambda function that creates a `set`. 
In [2]: grouped = df.groupby('language').agg({'static': 'min', 'keys': lambda x: set(x)}) 

# Then we get the missing keys... 
In [3]: missing = (grouped['keys']['en'] - grouped['keys']) 

# ... and count them 
In [4]: missing_counts = missing.apply(len).rename('# Missing') 

# Then we join all of this together and replace the keys with a joined string. 
In [5]: grouped.drop('keys', axis=1).join(missing_counts).join(missing.apply(', '.join)).reset_index() 
Out[5]: 
    language static # Missing   keys 
0  de  x   0 
1  en  x   0 
2  nl  x   1   key_3 
3  ua  x   2 key_2, key_3 
1

、あなたの質問にRタグを入れているので、ここではtidyrdplyrでそれを行う方法は次のとおりです。

ファンタスティック
library(dplyr);library(tidyr) 
df %>% 
    complete(nesting(static, langs), keys) %>% 
    group_by(langs)%>% 
    summarise(Static=max(static), 
      Missing=sum(is.na(values)), 
      Keys=toString(keys[is.na(values)]) 
      ) 

    langs Static Missing   Keys 
    <chr> <chr> <int>  <chr> 
1 de  x  0    
2 en  x  0    
3 nl  x  1  key_3 
4 ua  x  2 key_2, key_3 

データ

df <- read.table(text="static langs keys values 
'x' 'en' 'key_1' 'value_en_1' 
'x' 'en' 'key_2' 'value_en_2' 
'x' 'en' 'key_3' 'value_en_3' 
'x' 'de' 'key_1' 'value_de_1' 
'x' 'de' 'key_2' 'value_de_2' 
'x' 'de' 'key_3' 'value_de_3' 
'x' 'nl' 'key_1' 'value_nl_1' 
'x' 'nl' 'key_2' 'value_nl_2' 
'x' 'ua' 'key_1' 'value_en_1'",header=TRUE,stringsAsFactors = FALSE) 
+0

ありがとう、これは素晴らしいです。私はまた側でdplyrを学んでいます、そして、私はパンダがRに基づいて建てられたと理解しています。 –

関連する問題