Introduction
The “DataFrame.drop()” method can be used to drop a column from a Snowflake Snowpark DataFrame. It returns a new DataFrame that excludes the columns with the specified names from the output.
Syntax
dataframe.drop('column_name')
Consider the following DataFrame as the source for demonstrating dropping a column in a Snowpark DataFrame.
data = [[1,'TONY',100],
[2,'STEVE',400],
[3,'BRUCE',300],
[4,'THOR',200]]
df = session.createDataFrame(data, schema=["ID","NAME","SALARY"])
df.show()
----------------------------
|"ID" |"NAME" |"SALARY" |
----------------------------
|1 |TONY |100 |
|2 |STEVE |400 |
|3 |BRUCE |300 |
|4 |THOR |200 |
----------------------------
1. Dropping a Column from a Snowpark DataFrame
The following code demonstrates how to drop a column named “SALARY” from the DataFrame “df” using the drop() method.
df1 = df.drop("SALARY")
df1.show()
-----------------
|"ID" |"NAME" |
-----------------
|1 |TONY |
|2 |STEVE |
|3 |BRUCE |
|4 |THOR |
-----------------
2. Dropping Multiple Columns from a Snowpark DataFrame
The following code demonstrates how to drop a multiple columns named “ID” and “SALARY” from the DataFrame “df” using the drop() method.
df2 = df.drop("ID","SALARY")
df2.show()
----------
|"NAME" |
----------
|TONY |
|STEVE |
|BRUCE |
|THOR |
----------
3. Dropping All Columns from a Snowpark DataFrame
Snowpark does not support dropping all columns from a DataFrame. If you try to drop all columns, it throws an error stating “Cannot drop all columns”.
The following code demonstrates that trying to drop all columns from a DataFrame results in an error.
>>> df.drop("ID","NAME","SALARY")
raise SnowparkClientExceptionMessages.DF_CANNOT_DROP_ALL_COLUMNS()
snowflake.snowpark.exceptions.SnowparkColumnException: (1101): Cannot drop all columns
Conclusion
In this article, we have learned how to drop columns from a Snowflake Snowpark DataFrame with examples. We also covered that dropping all columns from a Snowpark DataFrame is not allowed and results in error.
Subscribe to our Newsletter !!
Related Articles:
- Introduction to Snowflake Snowpark for Python
- HOW TO: Create and Read Data from Snowflake Snowpark DataFrames?
- HOW TO: Write data into Snowflake from a Snowpark DataFrame?
- HOW TO: COPY Data from CSV Files INTO Snowflake Table using Snowpark?
- HOW TO: Add a New Column to a Snowpark DataFrame?
- HOW TO: Drop a Column from a Snowpark DataFrame?
- HOW TO: Remove Duplicates in a Snowflake Snowpark DataFrame?
- HOW TO: Update a DataFrame in Snowflake Snowpark?
- HOW TO: Merge two DataFrames in Snowflake Snowpark?
- HOW TO: Execute SQL Statements in Snowflake Snowpark?
- Aggregate Functions in Snowflake Snowpark
- GROUP BY in Snowflake Snowpark
- Joins in Snowflake Snowpark
- IN Operator in Snowflake Snowpark
- Window Functions in Snowflake Snowpark
- CASE Statement in Snowflake Snowpark
- UDFs in Snowflake Snowpark