Pyspark:选择特定列及其位置 [英] Pyspark : select specific column with its position

查看:26
本文介绍了Pyspark:选择特定列及其位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在数据框中选择带有编号而不是名称的特定列?

I would like to know how to select a specific column with its number but not with its name in a dataframe ?

在 Pandas 中是这样的:

Like this in Pandas:

df = df.iloc[:,2]

有可能吗?

推荐答案

你总是可以用 df.columns[n] 然后select 得到列的名称它:

You can always get the name of the column with df.columns[n] and then select it:

df = spark.createDataFrame([[1,2], [3,4]], ['a', 'b'])

选择n位置的列:

n = 1
df.select(df.columns[n]).show()
+---+                                                                           
|  b|
+---+
|  2|
|  4|
+---+

要选择除n列之外的所有内容:

To select all but column n:

n = 1

你可以使用drop:

df.drop(df.columns[n]).show()
+---+
|  a|
+---+
|  1|
|  3|
+---+

或者选择手动构造的列名:

Or select with manually constructed column names:

df.select(df.columns[:n] + df.columns[n+1:]).show()
+---+
|  a|
+---+
|  1|
|  3|
+---+

这篇关于Pyspark:选择特定列及其位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆