Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

334
Views
¿Cómo calcular los recuentos de cada valor distinto en un marco de datos pyspark?

Tengo una columna llena con un montón de iniciales de estados como cadenas. Mi objetivo es cómo el recuento de cada estado en dicha lista.

Por ejemplo: (("TX":3),("NJ":2)) debe ser la salida cuando hay dos ocurrencias de "TX" y "NJ" .

Soy bastante nuevo en pyspark, así que estoy perplejo con este problema. Cualquier ayuda sería muy apreciada.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Creo que está buscando usar el lenguaje DataFrame de groupBy y count .

Por ejemplo, dada la siguiente trama de datos, un estado por fila:

 df = sqlContext.createDataFrame([('TX',), ('NJ',), ('TX',), ('CA',), ('NJ',)], ('state',)) df.show() +-----+ |state| +-----+ | TX| | NJ| | TX| | CA| | NJ| +-----+

Los siguientes rendimientos:

 df.groupBy('state').count().show() +-----+-----+ |state|count| +-----+-----+ | TX| 2| | NJ| 2| | CA| 1| +-----+-----+
over 4 years ago · Santiago Trujillo Report

0

import pandas as pd import pyspark.sql.functions as F def value_counts(spark_df, colm, order=1, n=10): """ Count top n values in the given column and show in the given order Parameters ---------- spark_df : pyspark.sql.dataframe.DataFrame Data colm : string Name of the column to count values in order : int, default=1 1: sort the column descending by value counts and keep nulls at top 2: sort the column ascending by values 3: sort the column descending by values 4: do 2 and 3 (combine top n and bottom n after sorting the column by values ascending) n : int, default=10 Number of top values to display Returns ---------- Value counts in pandas dataframe """ if order==1 : return pd.DataFrame(spark_df.select(colm).groupBy(colm).count().orderBy(F.desc_nulls_first("count")).head(n),columns=["value","count"]) if order==2 : return pd.DataFrame(spark_df.select(colm).groupBy(colm).count().orderBy(F.asc(colm)).head(n),columns=["value","count"]) if order==3 : return pd.DataFrame(spark_df.select(colm).groupBy(colm).count().orderBy(F.desc(colm)).head(n),columns=["value","count"]) if order==4 : return pd.concat([pd.DataFrame(spark_df.select(colm).groupBy(colm).count().orderBy(F.asc(colm)).head(n),columns=["value","count"]), pd.DataFrame(spark_df.select(colm).groupBy(colm).count().orderBy(F.desc(colm)).head(n),columns=["value","count"])])
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!