Your data:
print df
DataFrame[col1: string, col2: string]
df.show()
+----+----+
|col1|col2|
+----+----+
| B| A|
| A| A|
| A| A|
| C| B|
| A| A|
+----+----+
diz = {"A":1, "B":2, "C":3}
Convert values of your dictionary from integer to string, in order to not get errors of replacing different types:
diz = {k:str(v) for k,v in zip(diz.keys(),diz.values())}
print diz
{'A': '1', 'C': '3', 'B': '2'}
Replace value of col1
df2 = df.na.replace(diz,1,"col1")
print df2
DataFrame[col1: string, col2: string]
df2.show()
+----+----+
|col1|col2|
+----+----+
| 2| A|
| 1| A|
| 1| A|
| 3| B|
| 1| A|
+----+----+
If you need to cast your values from String to Integer
from pyspark.sql.types import *
df3 = df2.select(df2["col1"].cast(IntegerType()),df2["col2"])
print df3
DataFrame[col1: int, col2: string]
df3.show()
+----+----+
|col1|col2|
+----+----+
| 2| A|
| 1| A|
| 1| A|
| 3| B|
| 1| A|
+----+----+
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…