gpt4 book ai didi

python - Pandas 到 PySpark : transforming a column of lists of tuples to separate columns for each tuple item

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:06 25 4
gpt4 key购买 nike

我需要转换一个 DataFrame,其中一列由元组列表组成,每个元组中的每个项目都必须是一个单独的列。

这是 Pandas 中的示例和解决方案:

import pandas as pd

df_dict = {
'a': {
"1": "stuff", "2": "stuff2"
},

"d": {
"1": [(1, 2), (3, 4)], "2": [(1, 2), (3, 4)]
}
}

df = pd.DataFrame.from_dict(df_dict)
print(df) # intial structure

a d
1 stuff [(1, 2), (3, 4)]
2 stuff2 [(1, 2), (3, 4)]

# first transformation, let's separate each list item into a new row
row_breakdown = df.set_index(["a"])["d"].apply(pd.Series).stack()
print(row_breakdown)

a
stuff 0 (1, 2)
1 (3, 4)
stuff2 0 (1, 2)
1 (3, 4)
dtype: object

row_breakdown = row_breakdown.reset_index().drop(columns=["level_1"])
print(row_breakdown)

a 0
0 stuff (1, 2)
1 stuff (3, 4)
2 stuff2 (1, 2)
3 stuff2 (3, 4)

# second transformation, let's get each tuple item into a separate column
row_breakdown.columns = ["a", "d"]
row_breakdown = row_breakdown["d"].apply(pd.Series)
row_breakdown.columns = ["value_1", "value_2"]
print(row_breakdown)

value_1 value_2
0 1 2
1 3 4
2 1 2
3 3 4

这是 Pandas 解决方案。我需要能够做同样的事情,但使用 PySpark (2.3)。我已经开始研究它,但很快就卡住了:

from pyspark.context import SparkContext, SparkConf
from pyspark.sql.session import SparkSession

conf = SparkConf().setAppName("appName").setMaster("local")
sc = SparkContext(conf=conf)

spark = SparkSession(sc)

df_dict = {
'a': {
"1": "stuff", "2": "stuff2"
},

"d": {
"1": [(1, 2), (3, 4)], "2": [(1, 2), (3, 4)]
}
}

df = pd.DataFrame(df_dict)
ddf = spark.createDataFrame(df)

row_breakdown = ddf.set_index(["a"])["d"].apply(pd.Series).stack()

AttributeError: 'DataFrame' object has no attribute 'set_index'

显然,Spark 不支持索引。任何指点表示赞赏。

最佳答案

这可能会:

from pyspark.context import SparkContext, SparkConf
from pyspark.sql.session import SparkSession
from pyspark.sql import functions as F
import pandas as pd

conf = SparkConf().setAppName("appName").setMaster("local")
sc = SparkContext(conf=conf)

spark = SparkSession(sc)

df_dict = {
'a': {
"1": "stuff", "2": "stuff2"
},

"d": {
"1": [(1, 2), (3, 4)], "2": [(1, 2), (3, 4)]
}
}

df = pd.DataFrame(df_dict)
ddf = spark.createDataFrame(df)


exploded = ddf.withColumn('d', F.explode("d"))
exploded.show()

结果:

+------+------+
| a| d|
+------+------+
| stuff|[1, 2]|
| stuff|[3, 4]|
|stuff2|[1, 2]|
|stuff2|[3, 4]|
+------+------+

为此,我觉得使用 SQL 更舒服:

exploded.createOrReplaceTempView("exploded")
spark.sql("SELECT a, d._1 as value_1, d._2 as value_2 FROM exploded").show()

重要说明:这是使用 _1 的原因和 _2访问器是因为 spark 将元组解析为结构并为其提供了默认键。如果在您的实际实现中,数据框包含 array<int> , 你应该使用 [0]语法。

最后的结果是:

+------+-------+-------+
| a|value_1|value_2|
+------+-------+-------+
| stuff| 1| 2|
| stuff| 3| 4|
|stuff2| 1| 2|
|stuff2| 3| 4|
+------+-------+-------+

关于python - Pandas 到 PySpark : transforming a column of lists of tuples to separate columns for each tuple item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52243200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com