Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pyspark dataframe数据类型在cast and descripe()和显示不同数据类型的数据类型之后未更改_Dataframe_Apache Spark_Pyspark - Fatal编程技术网

Pyspark dataframe数据类型在cast and descripe()和显示不同数据类型的数据类型之后未更改

Pyspark dataframe数据类型在cast and descripe()和显示不同数据类型的数据类型之后未更改,dataframe,apache-spark,pyspark,Dataframe,Apache Spark,Pyspark,我正在学习pySpark。我正试图收集一些数据。下面是我试过的代码 from pyspark.sql import SparkSession from pyspark.sql.types import IntegerType spark = SparkSession.builder.appName("learning").master("local[*]").getOrCreate() path = "deliveries.csv" text_df = spark.read.csv(pat

我正在学习pySpark。我正试图收集一些数据。下面是我试过的代码

from pyspark.sql import SparkSession
from pyspark.sql.types import IntegerType

spark = SparkSession.builder.appName("learning").master("local[*]").getOrCreate()

path = "deliveries.csv"

text_df = spark.read.csv(path,sep=",", header=True)

temp_df = text_df.withColumn("runs", text_df["batsman_runs"].cast(IntegerType()))
temp_df.show()
temp_df.cache()

print(temp_df.describe())
print(temp_df.dtypes)

temp_df.groupby('batsman').agg(sum('runs')).show()
下面是添加了列“runs”的文件中的数据

+--------+------+-------------------+--------------------+----+----+------------+------------+-----------+-------------+---------+--------+-----------+-----------+------------+------------+----------+----------+----------------+--------------+-------------+----+
|match_id|inning|       batting_team|        bowling_team|over|ball|     batsman| non_striker|     bowler|is_super_over|wide_runs|bye_runs|legbye_runs|noball_runs|penalty_runs|batsman_runs|extra_runs|total_runs|player_dismissed|dismissal_kind|      fielder|runs|
+--------+------+-------------------+--------------------+----+----+------------+------------+-----------+-------------+---------+--------+-----------+-----------+------------+------------+----------+----------+----------------+--------------+-------------+----+
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   1|   1|   DA Warner|    S Dhawan|   TS Mills|            0|        0|       0|          0|          0|           0|           0|         0|         0|            null|          null|         null|   0|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   1|   2|   DA Warner|    S Dhawan|   TS Mills|            0|        0|       0|          0|          0|           0|           0|         0|         0|            null|          null|         null|   0|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   1|   3|   DA Warner|    S Dhawan|   TS Mills|            0|        0|       0|          0|          0|           0|           4|         0|         4|            null|          null|         null|   4|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   1|   4|   DA Warner|    S Dhawan|   TS Mills|            0|        0|       0|          0|          0|           0|           0|         0|         0|            null|          null|         null|   0|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   1|   5|   DA Warner|    S Dhawan|   TS Mills|            0|        2|       0|          0|          0|           0|           0|         2|         2|            null|          null|         null|   0|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   1|   6|    S Dhawan|   DA Warner|   TS Mills|            0|        0|       0|          0|          0|           0|           0|         0|         0|            null|          null|         null|   0|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   1|   7|    S Dhawan|   DA Warner|   TS Mills|            0|        0|       0|          1|          0|           0|           0|         1|         1|            null|          null|         null|   0|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   2|   1|    S Dhawan|   DA Warner|A Choudhary|            0|        0|       0|          0|          0|           0|           1|         0|         1|            null|          null|         null|   1|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   2|   2|   DA Warner|    S Dhawan|A Choudhary|            0|        0|       0|          0|          0|           0|           4|         0|         4|            null|          null|         null|   4|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   2|   3|   DA Warner|    S Dhawan|A Choudhary|            0|        0|       0|          0|          1|           0|           0|         1|         1|            null|          null|         null|   0|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   2|   4|   DA Warner|    S Dhawan|A Choudhary|            0|        0|       0|          0|          0|           0|           6|         0|         6|            null|          null|         null|   6|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   2|   5|   DA Warner|    S Dhawan|A Choudhary|            0|        0|       0|          0|          0|           0|           0|         0|         0|       DA Warner|        caught|Mandeep Singh|   0|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   2|   6|MC Henriques|    S Dhawan|A Choudhary|            0|        0|       0|          0|          0|           0|           0|         0|         0|            null|          null|         null|   0|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   2|   7|MC Henriques|    S Dhawan|A Choudhary|            0|        0|       0|          0|          0|           0|           4|         0|         4|            null|          null|         null|   4|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   3|   1|    S Dhawan|MC Henriques|   TS Mills|            0|        0|       0|          0|          0|           0|           1|         0|         1|            null|          null|         null|   1|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   3|   2|MC Henriques|    S Dhawan|   TS Mills|            0|        0|       0|          0|          0|           0|           0|         0|         0|            null|          null|         null|   0|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   3|   3|MC Henriques|    S Dhawan|   TS Mills|            0|        0|       0|          0|          0|           0|           0|         0|         0|            null|          null|         null|   0|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   3|   4|MC Henriques|    S Dhawan|   TS Mills|            0|        0|       0|          0|          0|           0|           3|         0|         3|            null|          null|         null|   3|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   3|   5|    S Dhawan|MC Henriques|   TS Mills|            0|        0|       0|          0|          0|           0|           1|         0|         1|            null|          null|         null|   1|
|       1|     1|Sunrisers Hyderabad|Royal Challengers...|   3|   6|MC Henriques|    S Dhawan|   TS Mills|            0|        0|       0|          0|          0|           0|           1|         0|         1|            null|          null|         null|   1|
+--------+------+-------------------+--------------------+----+----+------------+------------+-----------+-------------+---------+--------+-----------+-----------+------------+------------+----------+----------+----------------+--------------+-------------+----+
我试图通过
batsman
获得
运行次数的总和。但是,我得到了下面的错误

Traceback (most recent call last):
  File "ipl.py", line 19, in <module>
    temp_df.groupby('batsman').agg(sum('runs')).show()
TypeError: unsupported operand type(s) for +: 'int' and 'str'
打印(临时数据类型)

为什么数据类型在强制转换后不转换?
为什么
描述
数据类型
显示不同?

错误原因是您没有导入求和函数。导入sum函数并重试,以上错误不会出现

from pyspark.sql.functions import sum
temp_df.groupby('batsman').agg(sum('runs')).show()

使用InferSchema true选项如何?如果不是,则将文件作为可下载的附件附加form@Mahesh古普塔:我试过你的建议,但问题还是一样。在数据类型中,转换为
int
的数据类型。但聚合仍然失败,出现了相同的错误。它的可能副本现在正在工作,甚至没有
cast
。它是从字符串自动转换为Int吗?我要提醒的是,如果您从pyspark.sql.functions import sum执行
,您将隐藏内置的
sum
。看看如果你这样做会发生什么。谢谢@pault。这清楚地解释了问题和答案的背景。python中的
sum
函数用于导致错误的执行。导入后,使用spark函数中的
sum
,解决了问题。此解决方案解决了问题。但是,为什么要在数据帧上描述()和数据类型来显示不同的数据类型呢?
[('match_id', 'string'), ('inning', 'string'), ('batting_team', 'string'), ('bowling_team', 'string'), ('over', 'string'), ('ball', 'string'), ('batsman', 'string'), ('non_striker', 'string'), ('bowler', 'string'), ('is_super_over', 'string'), ('wide_runs', 'string'), ('bye_runs', 'string'), ('legbye_runs', 'string'), ('noball_runs', 'string'), ('penalty_runs', 'string'), ('batsman_runs', 'string'), ('extra_runs', 'string'), ('total_runs', 'string'), ('player_dismissed', 'string'), ('dismissal_kind', 'string'), ('fielder', 'string'), ('runs', 'int')]
from pyspark.sql.functions import sum
temp_df.groupby('batsman').agg(sum('runs')).show()