PySpark DataFrames परिचय

PySpark परिचय

Benjamin Schmidt

Data Engineer

DataFrames के बारे में

  • DataFrames: सारणीबद्ध फॉर्मेट (rows/columns)
  • SQL जैसे ऑपरेशंस सपोर्ट
  • Pandas DataFrame या SQL TABLE जैसा
  • स्ट्रक्चर्ड डेटा

Dataframes

PySpark परिचय

Filestores से DataFrames बनाना

# Create a DataFrame from CSV
census_df = spark.read.csv('path/to/census.csv', header=True, inferSchema=True)
PySpark परिचय

DataFrame प्रिंट करना

# Show the first 5 rows of the DataFrame
census_df.show()


   age  education.num marital.status         occupation income
0   90              9        Widowed                  ?  <=50K
1   82              9        Widowed    Exec-managerial  <=50K
2   66             10        Widowed                  ?  <=50K
3   54              4       Divorced  Machine-op-inspct  <=50K
4   41             10      Separated     Prof-specialty  <=50K
PySpark परिचय

DataFrame Schema प्रिंट करना

# Show the schema
census_df.printSchema()

Output: root |-- age: integer (nullable = true) |-- education.num: integer (nullable = true) |-- marital.status: string (nullable = true) |-- occupation: string (nullable = true) |-- income: string (nullable = true)
PySpark परिचय

PySpark DataFrames पर बेसिक एनालिटिक्स

# .count() will return the total row numbers in the DataFrame
row_count = census_df.count()
print(f'Number of rows: {row_count}')
# groupby() allows the use of sql-like aggregations
census_df.groupBy('gender').agg({'salary_usd': 'avg'}).show()

अन्य aggregate functions हैं:

  • sum()
  • min()
  • max()
PySpark परिचय

PySpark एनालिटिक्स के मुख्य फंक्शंस

  • .select(): DataFrame से चुने हुए कॉलम्स निकालें
  • .filter(): शर्तों के आधार पर rows फ़िल्टर करें
  • .groupBy(): एक या अधिक कॉलम्स पर rows ग्रुप करें
  • .agg(): ग्रुप्ड डेटा पर aggregate functions लागू करें
PySpark परिचय

मुख्य फंक्शंस: उदाहरण

# Using filter and select, we can narrow down our DataFrame
filtered_census_df = census_df.filter(df['age'] > 50).select('age', 'occupation')
filtered_census_df.show()

Output +---+------------------+ |age| occupation | +---+------------------+ | 90| ?| | 82| Exec-managerial| | 66| ?| | 54| Machine-op-inspct| +---+------------------+
PySpark परिचय

अभ्यास करते हैं!

PySpark परिचय

Preparing Video For Download...