PySpark DataFrames के साथ इंटरैक्ट करना

PySpark के साथ Big Data Fundamentals

Upendra Devisetty

Science Analyst, CyVerse

PySpark में DataFrame ऑपरेटर्स

  • DataFrame ऑपरेशन: Transformations और Actions

  • DataFrame Transformations:

    • select(), filter(), groupby(), orderby(), dropDuplicates(), और withColumnRenamed()
  • DataFrame Actions:

    • printSchema(), head(), show(), count(), columns, और describe()

    Correction: printSchema() किसी भी Spark dataset/dataframe की method है, action नहीं

PySpark के साथ Big Data Fundamentals

select() और show() ऑपरेशन

  • select() transformation DataFrame में कॉलम का subset चुनता है
df_id_age = test.select('Age')
  • show() action DataFrame की पहली 20 पंक्तियाँ प्रिंट करता है
df_id_age.show(3)
+---+
|Age|
+---+
| 17|
| 17|
| 17|
+---+
only showing top 3 rows
PySpark के साथ Big Data Fundamentals

filter() और show() ऑपरेशन

  • filter() transformation शर्त के आधार पर पंक्तियाँ फ़िल्टर करता है
new_df_age21 = new_df.filter(new_df.Age > 21)
new_df_age21.show(3)
+-------+------+---+
|User_ID|Gender|Age|
+-------+------+---+
|1000002|     M| 55|
|1000003|     M| 26|
|1000004|     M| 46|
+-------+------+---+
only showing top 3 rows
PySpark के साथ Big Data Fundamentals

groupby() और count() ऑपरेशन

  • groupby() ऑपरेशन किसी वैरिएबल के अनुसार ग्रुप कर सकता है
test_df_age_group = test_df.groupby('Age')
test_df_age_group.count().show(3)
+---+------+
|Age| count|
+---+------+
| 26|219587|
| 17|     4|
| 55| 21504|
+---+------+
only showing top 3 rows
PySpark के साथ Big Data Fundamentals

orderby() Transformations

  • orderby() ऑपरेशन एक या अधिक कॉलम के आधार पर DataFrame को sort करता है
test_df_age_group.count().orderBy('Age').show(3)
+---+-----+
|Age|count|
+---+-----+
|  0|15098|
| 17|    4|
| 18|99660|
+---+-----+
only showing top 3 rows
PySpark के साथ Big Data Fundamentals

dropDuplicates()

  • dropDuplicates() DataFrame की डुप्लिकेट पंक्तियाँ हटाता है
test_df_no_dup = test_df.select('User_ID','Gender', 'Age').dropDuplicates()
test_df_no_dup.count()
5892
PySpark के साथ Big Data Fundamentals

withColumnRenamed Transformations

  • withColumnRenamed() DataFrame में किसी कॉलम का नाम बदलता है
test_df_sex = test_df.withColumnRenamed('Gender', 'Sex')
test_df_sex.show(3)
+-------+---+---+
|User_ID|Sex|Age|
+-------+---+---+
|1000001|  F| 17|
|1000001|  F| 17|
|1000001|  F| 17|
+-------+---+---+
PySpark के साथ Big Data Fundamentals

printSchema()

  • printSchema() ऑपरेशन DataFrame में कॉलम के टाइप प्रिंट करता है
test_df.printSchema()
 |-- User_ID: integer (nullable = true)
 |-- Product_ID: string (nullable = true)
 |-- Gender: string (nullable = true)
 |-- Age: string (nullable = true)
 |-- Occupation: integer (nullable = true)
 |-- Purchase: integer (nullable = true)
PySpark के साथ Big Data Fundamentals

columns actions

  • columns ऑपरेटर DataFrame के कॉलम प्रिंट करता है
test_df.columns
['User_ID', 'Gender', 'Age']
PySpark के साथ Big Data Fundamentals

describe() actions

  • describe() ऑपरेशन DataFrame में numerical कॉलम का summary statistics compute करता है
test_df.describe().show()
+-------+------------------+------+------------------+
|summary|           User_ID|Gender|               Age|
+-------+------------------+------+------------------+
|  count|            550068|550068|            550068|
|   mean|1003028.8424013031|  null|30.382052764385495|
| stddev|1727.5915855307312|  null|11.866105189533554|
|    min|           1000001|     F|                 0|
|    max|           1006040|     M|                55|
+-------+------------------+------+------------------+
PySpark के साथ Big Data Fundamentals

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

PySpark के साथ Big Data Fundamentals

Preparing Video For Download...