本文整理匯總了Python中pyspark.sql.functions.count方法的典型用法代碼示例。如果您正苦於以下問題:Python functions.count方法的具體用法?Python functions.count怎麽用?Python functions.count使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pyspark.sql.functions
的用法示例。
在下文中一共展示了functions.count方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: smvPivotSum
# 需要導入模塊: from pyspark.sql import functions [as 別名]
# 或者: from pyspark.sql.functions import count [as 別名]
def smvPivotSum(self, pivotCols, valueCols, baseOutput):
"""Perform SmvPivot, then sum the results.
Please refer smvPivot's document for context and details of the SmvPivot operation.
Args:
pivotCols (list(list(str))): list of lists of column names to pivot
valueCols (list(string)): names of value columns to sum
baseOutput (list(str)): expected names pivoted column
Examples:
For example, given a DataFrame df that represents the table
+-----+-------+---------+-------+
| id | month | product | count |
+=====+=======+=========+=======+
| 1 | 5/14 | A | 100 |
+-----+-------+---------+-------+
| 1 | 6/14 | B | 200 |
+-----+-------+---------+-------+
| 1 | 5/14 | B | 300 |
+-----+-------+---------+-------+
we can use
>>> df.smvGroupBy("id").smvPivotSum([["month", "product"]], ["count"], ["5_14_A", "5_14_B", "6_14_A", "6_14_B"])
to produce the following output
+-----+--------------+--------------+--------------+--------------+
| id | count_5_14_A | count_5_14_B | count_6_14_A | count_6_14_B |
+=====+==============+==============+==============+==============+
| 1 | 100 | 300 | NULL | 200 |
+-----+--------------+--------------+--------------+--------------+
Returns:
(DataFrame): result of pivot sum
"""
return DataFrame(self.sgd.smvPivotSum(smv_copy_array(self.df._sc, *pivotCols), smv_copy_array(self.df._sc, *valueCols), smv_copy_array(self.df._sc, *baseOutput)), self.df.sql_ctx)
示例2: smvSelectPlus
# 需要導入模塊: from pyspark.sql import functions [as 別名]
# 或者: from pyspark.sql.functions import count [as 別名]
def smvSelectPlus(self, *cols):
"""Selects all the current columns in current DataFrame plus the supplied expressions
The new columns are added to the end of the current column list.
Args:
cols (\*Column): expressions to add to the DataFrame
Example:
>>> df.smvSelectPlus((col("price") * col("count")).alias("amt"))
Returns:
(DataFrame): the resulting DataFrame after removal of columns
"""
jdf = self._jDfHelper.smvSelectPlus(_to_seq(cols, _jcol))
return DataFrame(jdf, self._sql_ctx)
示例3: smvDupeCheck
# 需要導入模塊: from pyspark.sql import functions [as 別名]
# 或者: from pyspark.sql.functions import count [as 別名]
def smvDupeCheck(self, keys, n=10000):
"""For a given list of potential keys, check for duplicated records with the number of duplications and all the columns.
Null values are allowed in the potential keys, so duplication on Null valued keys will also be reported.
Args:
keys (list(string)): the key column list which the duplicate check applied
n (integer): number of rows from input data for checking duplications, defaults to 10000
Returns:
(DataFrame): returns key columns + "_N" + the rest columns for the records with more key duplication records,
where "_N" has the count of duplications of the key values of that record
"""
dfTopN = self.df.limit(n).cache()
res = dfTopN.groupBy(*keys)\
.agg(F.count(F.lit(1)).alias('_N'))\
.where(F.col('_N') > 1)\
.smvJoinByKey(dfTopN, keys, 'inner', True)\
.orderBy(*keys)
dfTopN.unpersist()
return res
示例4: get_playcounts_df
# 需要導入模塊: from pyspark.sql import functions [as 別名]
# 或者: from pyspark.sql.functions import count [as 別名]
def get_playcounts_df(listens_df, recordings_df, users_df, metadata):
""" Prepare playcounts dataframe.
Args:
listens_df : Dataframe containing recording_mbids corresponding to a user.
recordings_df : Dataframe containing distinct recordings and corresponding
mbids and names.
users_df : Dataframe containing user names and user ids.
Returns:
playcounts_df: Dataframe containing play(listen) counts of users.
"""
# listens_df is joined with users_df on user_name.
# The output is then joined with recording_df on recording_mbid.
# The final step uses groupBy which create groups on user_id and recording_id and counts the number of recording_ids.
# The final dataframe tells us about the number of times a user has listend to a particular track for all users.
playcounts_df = listens_df.join(users_df, 'user_name', 'inner') \
.join(recordings_df, 'mb_recording_mbid', 'inner') \
.groupBy('user_id', 'recording_id') \
.agg(func.count('recording_id').alias('count'))
metadata['playcounts_count'] = playcounts_df.count()
save_dataframe(playcounts_df, path.PLAYCOUNTS_DATAFRAME_PATH)
return playcounts_df
示例5: get_recordings_df
# 需要導入模塊: from pyspark.sql import functions [as 別名]
# 或者: from pyspark.sql.functions import count [as 別名]
def get_recordings_df(mapped_listens_df, metadata):
""" Prepare recordings dataframe.
Args:
mapped_listens_df (dataframe): listens mapped with msid_mbid_mapping.
Returns:
recordings_df: Dataframe containing distinct recordings and corresponding
mbids and names.
"""
recording_window = Window.orderBy('mb_recording_mbid')
recordings_df = mapped_listens_df.select('mb_artist_credit_id',
'mb_artist_credit_mbids',
'mb_recording_mbid',
'mb_release_mbid',
'msb_artist_credit_name_matchable',
'track_name') \
.distinct() \
.withColumn('recording_id', rank().over(recording_window))
metadata['recordings_count'] = recordings_df.count()
save_dataframe(recordings_df, path.RECORDINGS_DATAFRAME_PATH)
return recordings_df
示例6: get_users_dataframe
# 需要導入模塊: from pyspark.sql import functions [as 別名]
# 或者: from pyspark.sql.functions import count [as 別名]
def get_users_dataframe(mapped_listens_df, metadata):
""" Prepare users dataframe
Args:
mapped_listens_df (dataframe): listens mapped with msid_mbid_mapping.
Returns:
users_df : Dataframe containing user names and user ids.
"""
# We use window function to give rank to distinct user_names
# Note that if user_names are not distinct rank would repeat and give unexpected results.
user_window = Window.orderBy('user_name')
users_df = mapped_listens_df.select('user_name').distinct() \
.withColumn('user_id', rank().over(user_window))
metadata['users_count'] = users_df.count()
save_dataframe(users_df, path.USERS_DATAFRAME_PATH)
return users_df
示例7: get_top_artists
# 需要導入模塊: from pyspark.sql import functions [as 別名]
# 或者: from pyspark.sql.functions import count [as 別名]
def get_top_artists(mapped_listens_subset, top_artist_limit):
""" Get top artists listened to by users who have a listening history in
the past X days where X = RECOMMENDATION_GENERATION_WINDOW.
Args:
df (dataframe): A subset of mapped_df containing user history.
top_artist_limit (int): number of top artist to calculate
Returns:
top_artists_df (dataframe): Top Y artists listened to by a user for all users where
Y = TOP_ARTISTS_LIMIT
"""
df = mapped_listens_subset.select('mb_artist_credit_id', 'msb_artist_credit_name_matchable', 'user_name') \
.groupBy('mb_artist_credit_id', 'msb_artist_credit_name_matchable', 'user_name') \
.agg(func.count('mb_artist_credit_id').alias('count'))
window = Window.partitionBy('user_name').orderBy(col('count').desc())
top_artists_df = df.withColumn('rank', row_number().over(window)) \
.where(col('rank') <= top_artist_limit) \
.select('mb_artist_credit_id', 'msb_artist_credit_name_matchable', 'user_name')
return top_artists_df
示例8: __init__
# 需要導入模塊: from pyspark.sql import functions [as 別名]
# 或者: from pyspark.sql.functions import count [as 別名]
def __init__(self, kdf_or_kser, window, min_periods=None):
from databricks.koalas import DataFrame, Series
if window < 0:
raise ValueError("window must be >= 0")
if (min_periods is not None) and (min_periods < 0):
raise ValueError("min_periods must be >= 0")
if min_periods is None:
# TODO: 'min_periods' is not equivalent in pandas because it does not count NA as
# a value.
min_periods = window
if not isinstance(kdf_or_kser, (DataFrame, Series)):
raise TypeError(
"kdf_or_kser must be a series or dataframe; however, got: %s" % type(kdf_or_kser)
)
window = Window.orderBy(NATURAL_ORDER_COLUMN_NAME).rowsBetween(
Window.currentRow - (window - 1), Window.currentRow
)
super(Rolling, self).__init__(kdf_or_kser, window, min_periods)
示例9: _summary
# 需要導入模塊: from pyspark.sql import functions [as 別名]
# 或者: from pyspark.sql.functions import count [as 別名]
def _summary(self, name=None):
"""
Return a summarized representation.
Parameters
----------
name : str
name to use in the summary representation
Returns
-------
String with a summarized representation of the index
"""
head, tail, total_count = self._internal.spark_frame.select(
F.first(self.spark.column), F.last(self.spark.column), F.count(F.expr("*"))
).first()
if total_count > 0:
index_summary = ", %s to %s" % (pprint_thing(head), pprint_thing(tail))
else:
index_summary = ""
if name is None:
name = type(self).__name__
return "%s: %s entries%s" % (name, total_count, index_summary)
示例10: count
# 需要導入模塊: from pyspark.sql import functions [as 別名]
# 或者: from pyspark.sql.functions import count [as 別名]
def count(self):
"""
Compute count of group, excluding missing values.
See Also
--------
databricks.koalas.Series.groupby
databricks.koalas.DataFrame.groupby
Examples
--------
>>> df = ks.DataFrame({'A': [1, 1, 2, 1, 2],
... 'B': [np.nan, 2, 3, 4, 5],
... 'C': [1, 2, 1, 1, 2]}, columns=['A', 'B', 'C'])
>>> df.groupby('A').count().sort_index() # doctest: +NORMALIZE_WHITESPACE
B C
A
1 2 3
2 2 2
"""
return self._reduce_for_stat_function(F.count, only_numeric=False)
# TODO: We should fix See Also when Series implementation is finished.
示例11: _make_plot
# 需要導入模塊: from pyspark.sql import functions [as 別名]
# 或者: from pyspark.sql.functions import count [as 別名]
def _make_plot(self):
# 'num_colors' requires to calculate `shape` which has to count all.
# Use 1 for now to save the computation.
colors = self._get_colors(num_colors=1)
stacking_id = self._get_stacking_id()
sdf = self.data._internal.spark_frame
for i, label in enumerate(self.data._internal.column_labels):
# 'y' is a Spark DataFrame that selects one column.
y = sdf.select(self.data._internal.spark_column_for(label))
ax = self._get_ax(i)
kwds = self.kwds.copy()
label = pprint_thing(label if len(label) > 1 else label[0])
kwds["label"] = label
style, kwds = self._apply_style_colors(colors, kwds, i, label)
if style is not None:
kwds["style"] = style
kwds = self._make_plot_keywords(kwds, y)
artists = self._plot(ax, y, column_num=i, stacking_id=stacking_id, **kwds)
self._add_legend_handle(artists[0], label, index=i)
示例12: count
# 需要導入模塊: from pyspark.sql import functions [as 別名]
# 或者: from pyspark.sql.functions import count [as 別名]
def count(self):
"""
Return number of non-NA/null observations in the Series.
Returns
-------
nobs : int
Examples
--------
Constructing DataFrame from a dictionary:
>>> df = ks.DataFrame({"Person":
... ["John", "Myla", "Lewis", "John", "Myla"],
... "Age": [24., np.nan, 21., 33, 26]})
Notice the uncounted NA values:
>>> df['Person'].count()
5
>>> df['Age'].count()
4
"""
return self._reduce_for_stat_function(Frame._count_expr, name="count")
示例13: stats
# 需要導入模塊: from pyspark.sql import functions [as 別名]
# 或者: from pyspark.sql.functions import count [as 別名]
def stats(self, columns):
"""Compute the stats for each column provided in columns.
Parameters
----------
columns : list of str, contains all columns to compute stats on.
"""
assert (not isinstance(columns, basestring)), "columns should be a " \
"list of strs, " \
"not a str!"
assert isinstance(columns, list), "columns should be a list!"
from pyspark.sql import functions as F
functions = [F.min, F.max, F.avg, F.count]
aggs = list(
self._flatmap(lambda column: map(lambda f: f(column), functions),
columns))
return PStats(self.from_schema_rdd(self._schema_rdd.agg(*aggs)))
示例14: count
# 需要導入模塊: from pyspark.sql import functions [as 別名]
# 或者: from pyspark.sql.functions import count [as 別名]
def count(self):
"""Compute the number of elements in each group."""
if self._can_use_new_school():
self._prep_spark_sql_groupby()
import pyspark.sql.functions as func
return self._use_aggregation(func.count)
self._prep_pandas_groupby()
myargs = self._myargs
mykwargs = self._mykwargs
def create_combiner(x):
return x.groupby(*myargs, **mykwargs).count()
def merge_value(x, y):
return x.append(create_combiner(y)).count()
def merge_combiner(x, y):
return x.append(y).count(level=0)
rddOfCounts = self._sortIfNeeded(self._distributedRDD.combineByKey(
create_combiner,
merge_value,
merge_combiner)).values()
return DataFrame.fromDataFrameRDD(rddOfCounts, self.sql_ctx)
示例15: _join_results
# 需要導入模塊: from pyspark.sql import functions [as 別名]
# 或者: from pyspark.sql.functions import count [as 別名]
def _join_results(self, scaffolds_df):
def _read_rows(row):
idx, _, dec = row.split("\t")
return ps.Row(id=idx, decoration_smi=dec)
sampled_df = SPARK.createDataFrame(SC.textFile(self._tmp_path(
"sampled_decorations"), self.num_partitions).map(_read_rows))
if self.decorator_type == "single":
processed_df = self._join_results_single(scaffolds_df, sampled_df)
elif self.decorator_type == "multi":
processed_df = self._join_results_multi(scaffolds_df, sampled_df)
else:
raise ValueError("decorator_type has an invalid value '{}'".format(self.decorator_type))
return processed_df\
.where("smiles IS NOT NULL")\
.groupBy("smiles")\
.agg(
psf.first("scaffold").alias("scaffold"),
psf.first("decorations").alias("decorations"),
psf.count("smiles").alias("count"))