如何通过列名的前缀对 spark 数据框进行子集化?

How to subset spark dataframe by prefixes of the column names?

我的 spark 数据框 df 的列名是:A_x1、A_x2、B_x1、B_x2、C_x1, C_x2.

如何使用前缀从 df 创建 3 个新的 spark 数据帧?输出应如下所示:

谢谢!

您可以使用 colRegex 来过滤列:

A_ = df.select(df.colRegex('`A_.*`'))
B_ = df.select(df.colRegex('`B_.*`'))
C_ = df.select(df.colRegex('`C_.*`'))