>>> a
DataFrame[id: bigint, julian_date: string, user_id: bigint]
>>> b
DataFrame[id: bigint, quan_created_money: decimal(10,0), quan_created_cnt: bigint]
>>> a.join(b, a.id==b.id, 'outer')
DataFrame[id: bigint, julian_date: string, user_id: bigint, id: bigint, quan_created_money: decimal(10,0), quan_created_cnt: bigint]
有两个id: bigint
,我想删除一个。我该怎么办?
答案 0 :(得分:81)
阅读Spark文档,我发现了一个更简单的解决方案。
从spark 1.4版开始,有一个函数drop(col)
可以在数据帧的pyspark中使用。
您可以通过两种方式使用它
df.drop('age').collect()
df.drop(df.age).collect()
答案 1 :(得分:22)
添加到@ Patrick的答案,您可以使用以下内容删除多列
columns_to_drop = ['id', 'id_copy']
df = df.drop(*columns_to_drop)
答案 2 :(得分:21)
执行此操作的一种简单方法是向用户“select
”显示,您可以获取columns
,dataframe
的所有df
列表,其中包含df.columns
1}}
drop_list = ['a column', 'another column', ...]
df.select([column for column in df.columns if column not in drop_list])
答案 3 :(得分:12)
您可以明确命名要保留的列,如下所示:
keep = [a.id, a.julian_date, a.user_id, b.quan_created_money, b.quan_created_cnt]
或者在更通用的方法中,您通过列表理解包括除特定列之外的所有列。例如像这样(不包括id
中的b
列):
keep = [a[c] for c in a.columns] + [b[c] for c in b.columns if c != 'id']
最后,您可以选择加入结果:
d = a.join(b, a.id==b.id, 'outer').select(*keep)
答案 4 :(得分:4)
您可以使用两种方式:
1: 您只需保留必要的列:
drop_column_list = ["drop_column"]
df = df.select([column for column in df.columns if column not in drop_column_list])
2:这是更优雅的方式。
df = df.drop("col_name")
您应该避免使用collect()版本,因为它将完整的数据集发送到主数据库,这将需要大量的计算工作!
答案 5 :(得分:3)
可能有点偏离主题,但这是使用Scala的解决方案。从Array
中创建oldDataFrame
列名称,然后删除要删除的列("colExclude")
。然后将Array[Column]
传递给select
并解压缩。
val columnsToKeep: Array[Column] = oldDataFrame.columns.diff(Array("colExclude"))
.map(x => oldDataFrame.col(x))
val newDataFrame: DataFrame = oldDataFrame.select(columnsToKeep: _*)
答案 6 :(得分:1)
是的,可以通过像这样切片来删除/选择列:
slice = data.columns[a:b]
data.select(slice).show()
示例:
newDF = spark.createDataFrame([
(1, "a", "4", 0),
(2, "b", "10", 3),
(7, "b", "4", 1),
(7, "d", "4", 9)],
("id", "x1", "x2", "y"))
slice = newDF.columns[1:3]
newDF.select(slice).show()
使用select方法获取特征列:
features = newDF.columns[:-1]
newDF.select(features).show()
使用 drop 方法获取最后一列:
last_col= newDF.drop(*features)
last_col.show()
答案 7 :(得分:0)
考虑2个dataFrames:
>>> aDF.show()
+---+----+
| id|datA|
+---+----+
| 1| a1|
| 2| a2|
| 3| a3|
+---+----+
和
>>> bDF.show()
+---+----+
| id|datB|
+---+----+
| 2| b2|
| 3| b3|
| 4| b4|
+---+----+
要完成您要寻找的东西,有两种方法:
1。加入条件不同。不用说aDF.id == bDF.id
aDF.join(bDF, aDF.id == bDF.id, "outer")
写下:
aDF.join(bDF, "id", "outer").show()
+---+----+----+
| id|datA|datB|
+---+----+----+
| 1| a1|null|
| 3| a3| b3|
| 2| a2| b2|
| 4|null| b4|
+---+----+----+
这将自动消除多余的删除过程。
2。使用别名:您将因此丢失与B特定ID相关的数据。
>>> from pyspark.sql.functions import col
>>> aDF.alias("a").join(bDF.alias("b"), aDF.id == bDF.id, "outer").drop(col("b.id")).show()
+----+----+----+
| id|datA|datB|
+----+----+----+
| 1| a1|null|
| 3| a3| b3|
| 2| a2| b2|
|null|null| b4|
+----+----+----+
答案 8 :(得分:0)
您可以这样删除列:
df.drop("column Name).columns
在您的情况下:
df.drop("id").columns
如果要删除多个列,可以执行以下操作:
dfWithLongColName.drop("ORIGIN_COUNTRY_NAME", "DEST_COUNTRY_NAME")