私は複数の列を持つSpark DataFrameを持っています。私は1つの列に基づいて行をグループ化し、各グループの2番目の列のモードを見つけるしたいと思います。パンダのデータフレームでの作業、私はこのような何かをするだろう:pyspark以内(py)でグループ化されたデータのモードSpark
rand_values = np.random.randint(max_value,
size=num_values).reshape((num_values/2, 2))
rand_values = pd.DataFrame(rand_values, columns=['x', 'y'])
rand_values['x'] = rand_values['x'] > max_value/2
rand_values['x'] = rand_values['x'].astype('int32')
print(rand_values)
## x y
## 0 0 0
## 1 0 4
## 2 0 1
## 3 1 1
## 4 1 2
def mode(series):
return scipy.stats.mode(series['y'])[0][0]
rand_values.groupby('x').apply(mode)
## x
## 0 4
## 1 1
## dtype: int64
、私はどのようにのために途方に暮れてよやって単一の列のモード
df = sql_context.createDataFrame(rand_values)
def mode_spark(df, column):
# Group by column and count the number of occurrences
# of each x value
counts = df.groupBy(column).count()
# - Find the maximum value in the 'counts' column
# - Join with the counts dataframe to select the row
# with the maximum count
# - Select the first element of this dataframe and
# take the value in column
mode = counts.join(
counts.agg(F.max('count').alias('count')),
on='count'
).limit(1).select(column)
return mode.first()[column]
mode_spark(df, 'x')
## 1
mode_spark(df, 'y')
## 1
を見つけることができていますその機能をグループ化されたデータに適用します。このロジックをDataFrameに直接適用することができない場合は、他の手段で同じ効果を得ることは可能ですか?
ありがとうございます!
グループを(x、y)で集計し、カウントで集計し、ここに示すように最大行を選択してください:http://stackoverflow.com/a/35226857/1560062 – zero323
クイック返信ありがとう!私はこれを試してみよう! – bjack3
それは働いたように見えます!私のソリューションを追加することは可能ですか?あなたを「回答者」としてマークしますか? – bjack3