DataFrames के साथ डेटा मैनिपुलेशन

PySpark परिचय

Ben Schmidt

Data Engineer

Missing data हैंडल करना

  • null मान वाली पंक्तियाँ हटाने के लिए .na.drop() का उपयोग करें
# Drop rows with any nulls
df_cleaned = df.na.drop()

# Filter out nulls df_cleaned = df.where(col("columnName").isNotNull())
  • किसी मान से null को बदलने के लिए .na.fill({"column": value) का उपयोग करें
# Fill nulls in the age column with the value 0
df_filled = df.na.fill({"age": 0})
PySpark परिचय

कॉलम ऑपरेशंस

  • गणना या मौजूदा कॉलम के आधार पर नया कॉलम जोड़ने के लिए .withColumn() का उपयोग करें
# Create a new column 'age_plus_5'
df = df.withColumn("age_plus_5", df["age"] + 5)
  • कॉलम का नाम बदलने के लिए withColumnRenamed() का उपयोग करें
# Rename the 'age' column to 'years'
df = df.withColumnRenamed("age", "years")
  • गैर-ज़रूरी कॉलम हटाने के लिए drop() का उपयोग करें
# Drop the 'department' column
df = df.drop("department")
PySpark परिचय

रो ऑपरेशंस

  • शर्तों के आधार पर पंक्तियाँ चुनने के लिए .filter() का उपयोग करें
# Filter rows where salary is greater than 50000
filtered_df = df.filter(df["salary"] > 50000)
  • डेटा को समरी करने के लिए .groupBy() और एग्रीगेट फंक्शन (जैसे, .sum(), .avg()) का उपयोग करें
# Group by department and calculate the average salary
grouped_df = df.groupBy("department").avg("salary")
PySpark परिचय

Row Operations के नतीजे

  • फ़िल्टरिंग

    +------+---+-----------------+
    |salary|age|      occupation |
    +------+---+-----------------+
    | 60000| 45|Exec-managerial  |
    | 70000| 35|Prof-specialty   |
    +------+---+-----------------+
    
  • GroupBy ` +----------+-----------+ |department|avg(salary)| +----------+-----------+ | HR| 80000.0| | IT| 70000.0| +----------+-----------+

`

PySpark परिचय

चीटशीट

# Drop rows with any nulls
df_cleaned = df.na.drop()

#Drop nulls on a column df_cleaned = df.where(col("columnName").isNotNull())
# Fill nulls in the age column with the value 0 df_filled = df.na.fill({"age": 0})
  • नए या मौजूदा कॉलम पर आधारित कॉलम जोड़ने के लिए .withColumn() का उपयोग करें. Syntax: .withColumn("new_col_name", "original transformation")

    # Create a new column 'age_plus_5'
    df = df.withColumn("age_plus_5", df["age"] + 5)
    
  • कॉलम का नाम बदलने के लिए withColumnRenamed() का उपयोग करें Syntax: withColumnRenamed(old column name,new column name`

# Rename the 'age' column to 'years'
df = df.withColumnRenamed("age", "years")
  • गैर-ज़रूरी कॉलम हटाने के लिए drop() का उपयोग करें Syntax: .drop(column name)
# Drop the 'department' column
df = df.drop("department")
# Filter rows where salary is greater than 50000
filtered_df = df.filter(df["salary"] > 50000)
  • डेटा को समरी करने के लिए .groupBy() और एग्रीगेट फंक्शन (जैसे, .sum(), .avg()) का उपयोग करें
# Group by department and calculate the average salary
grouped_df = df.groupBy("department").avg("salary")
PySpark परिचय

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

PySpark परिचय

Preparing Video For Download...