如何将数据框的列映射到新的列名

huangapple go评论50阅读模式
英文:

How to create mapping of dataframe columns with new column names

问题

col_map = {"name": "new_name", "age": "new_age"}

英文:

I want to create a column mapping for dataframe columns, this mapping depending on the dataframe schema.

eg.

df = spark.createDataFrame([("Alice", 2), ("Bob", 5)], ("name", "age"))
df.show()
+-----+---+
| name|age|
+-----+---+
|Alice|  2|
|  Bob|  5|
+-----+---+

I want to create map with renamed column. I want to use this map to extract the renamed names in the further code.(I have to create mapping because my schema is dynamic.)

col_map={"name":"new_name","age":"new_age"}

when ever I df in my further code I should be always able to read columns new_name and new_age etc

答案1

得分: 2

我们可以使用 .alias() 来动态更改数据框列名,基于 map

示例:

from pyspark.sql.functions import *
df = spark.createDataFrame([("Alice", 2), ("Bob", 5)], ("name", "age"))

df.show(10, False)

col_map={"name":"new_name","age":"new_age"}

df.select([col(f).alias(col_map[f'{f}']) for f in df.columns]).show(10, False)

输出:

+-----+---+
|name |age|
+-----+---+
|Alice|2  |
|Bob  |5  |
+-----+---+

+--------+-------+
|new_name|new_age|
+--------+-------+
|Alice   |2      |
|Bob     |5      |
+--------+-------+
英文:

We can use .alias() for dynamically change the dataframe column names based on the map.

Example:

from pyspark.sql.functions import *
df = spark.createDataFrame([("Alice", 2), ("Bob", 5)], ("name", "age"))

df.show(10,False)

col_map={"name":"new_name","age":"new_age"}

df.select([col(f).alias(col_map[f'{f}']) for f in df.columns]).show(10,False)
#
#+-----+---+
#|name |age|
#+-----+---+
#|Alice|2  |
#|Bob  |5  |
#+-----+---+
#
#+--------+-------+
#|new_name|new_age|
#+--------+-------+
#|Alice   |2      |
#|Bob     |5      |
#+--------+-------+

huangapple
  • 本文由 发表于 2023年7月24日 16:30:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定