xjx*_*524 61 apache-spark apache-spark-sql pyspark
>>> 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]
Run Code Online (Sandbox Code Playgroud)
有两个id: bigint,我想删除一个.我能怎么做?
Pat*_* C. 95
阅读Spark文档我发现了一个更简单的解决方案.
从1.4版本的spark开始,有一个函数drop(col)可以在数据帧的pyspark中使用.
您可以通过两种方式使用它
df.drop('age').collect()df.drop(df.age).collect()Clo*_*ave 37
添加到@ Patrick的答案,您可以使用以下内容删除多个列
columns_to_drop = ['id', 'id_copy']
df = df.drop(*columns_to_drop)
Run Code Online (Sandbox Code Playgroud)
小智 22
一个简单的方法做,这是对用户" select",并意识到你可以得到所有的列表columns为dataframe,df与df.columns
drop_list = ['a column', 'another column', ...]
df.select([column for column in df.columns if column not in drop_list])
Run Code Online (Sandbox Code Playgroud)
kar*_*son 11
您可以显式命名要保留的列,如下所示:
keep = [a.id, a.julian_date, a.user_id, b.quan_created_money, b.quan_created_cnt]
Run Code Online (Sandbox Code Playgroud)
或者在更通用的方法中,您可以通过列表理解包括除特定列之外的所有列.例如像这样(不包括id列b):
keep = [a[c] for c in a.columns] + [b[c] for c in b.columns if c != 'id']
Run Code Online (Sandbox Code Playgroud)
最后,您可以选择加入结果:
d = a.join(b, a.id==b.id, 'outer').select(*keep)
Run Code Online (Sandbox Code Playgroud)
您可以使用两种方式:
1:您只保留必要的列:
drop_column_list = ["drop_column"]
df = df.select([column for column in df.columns if column not in drop_column_list])
Run Code Online (Sandbox Code Playgroud)
2:这是更优雅的方式。
df = df.drop("col_name")
Run Code Online (Sandbox Code Playgroud)
您应该避免使用collect()版本,因为它将完整的数据集发送到主数据库,这将需要大量的计算工作!
| 归档时间: |
|
| 查看次数: |
125897 次 |
| 最近记录: |