从巨大的数据框中删除重复的列

问题描述 投票:0回答:1

假设我有巨大的数据帧,并且我使用 pyspark 中的“左”连接来加入它们。现在加入他们后,我发现一些列名称被重复,因为同一列存在于多个数据框中。识别这些重复列并删除它们的最佳和最有效的技术应该是什么?我知道我可以自己检查数据框,或者也许只选择我需要的列,但如果数据框太大而看不到每一列,也许我想打印每个数据框中存在的每一列,但条件是没有列应该出现,该怎么办被重复。请帮忙。

所以我使用 select 方法得到了我想要的结果,我得到了正确的输出,但是如果我需要所有列并且它们都不应该是重复的怎么办?

dataframe pyspark apache-spark-sql duplicates spark-streaming
1个回答
0
投票

在进行连接之前,重命名列,例如

df1.col_i
df2.col_j
,然后仅选择所需的列。如果需要,在加入数据框后重命名选定的列。下面给出示例代码。

data1 = [
    ["01001", 1, 5, 10, 200, 100, 300, 400],
    ["01001", 2, 1, 2, 100, 200, 300, 400],
    ["01003", 1, 5, 10, 200, 100, 300, 400],
    ["01003", 2, 1, 2, 100, 200, 300, 400],

      ]

df1Columns = ["county", "bin_number",   "age_group1",   "age_group2",   "t_group1_1",   "t_group2_1",   "t_group1_2",   "t_group2_2"]
df1 = sqlContext.createDataFrame(data=data1, schema = df1Columns)

columns_list = df1.columns
print("columns list of dataframes")
print(columns_list)
print("Given dataframe")
df1.show(n=100, truncate=False)


columns_list_new = ["tableA." + name for name in columns_list ]
df1_mod = df1.toDF(*columns_list_new)

print("columns list of modified dataframes")
print(df1_mod.columns)
print("Printing modified dataframe")
df1_mod.show(n=100, truncate=False)

输出:

columns list of dataframes
['county', 'bin_number', 'age_group1', 'age_group2', 't_group1_1', 't_group2_1', 't_group1_2', 't_group2_2']
Given dataframe
+------+----------+----------+----------+----------+----------+----------+----------+
|county|bin_number|age_group1|age_group2|t_group1_1|t_group2_1|t_group1_2|t_group2_2|
+------+----------+----------+----------+----------+----------+----------+----------+
|01001 |1         |5         |10        |200       |100       |300       |400       |
|01001 |2         |1         |2         |100       |200       |300       |400       |
|01003 |1         |5         |10        |200       |100       |300       |400       |
|01003 |2         |1         |2         |100       |200       |300       |400       |
+------+----------+----------+----------+----------+----------+----------+----------+

columns list of modified dataframes
['tableA.county', 'tableA.bin_number', 'tableA.age_group1', 'tableA.age_group2', 'tableA.t_group1_1', 'tableA.t_group2_1', 'tableA.t_group1_2', 'tableA.t_group2_2']
Printing modified dataframe
+-------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+
|tableA.county|tableA.bin_number|tableA.age_group1|tableA.age_group2|tableA.t_group1_1|tableA.t_group2_1|tableA.t_group1_2|tableA.t_group2_2|
+-------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+
|01001        |1                |5                |10               |200              |100              |300              |400              |
|01001        |2                |1                |2                |100              |200              |300              |400              |
|01003        |1                |5                |10               |200              |100              |300              |400              |
|01003        |2                |1                |2                |100              |200              |300              |400              |
+-------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+
© www.soinside.com 2019 - 2024. All rights reserved.