我在下面使用pyspark2.1是我的数据帧
YesterdayDataDF
1,Nagraj,Keshav,2017-11-20 00:02:39.867000000,2017-11-20 00:02:39.867000000,20171120060038
2,Raghu,HR,2017-11-20 00:02:39.867000000,2017-11-20 00:02:39.867000000,20171120060038
TodayDataDF
1,Nagraj,K,2017-11-21 00:02:39.867000000,2017-11-21 00:02:39.867000000
2,Raghu,HR,2017-11-21 00:02:39.867000000,2017-11-20 00:02:39.867000000
3,Ramya,Govindaraju,2017-11-21 00:02:39.867000000,2017-11-20 00:02:39.867000000
我的输出
1,Nagraj,K,2017-11-21 00:02:39.867000000,2017-11-20 00:02:39.867000000,20171120060038
3,Ramya,Govindaraju,2017-11-21 00:02:39.867000000,2017-11-20 00:02:39.867000000,20171120060038
我不应该获得两个数据帧中都存在的记录,因为名称中只有第一条记录发生变化我应该获得此记录而记录号3是新记录。
我使用了以下逻辑
df =today_data_df.select("id").subtract(yesterdata_data_df.select("id")).toDF('d1').join(today_data_df,col('d1')==today_data_df.id).drop('d1')
输出是:
3,Ramya,Govindaraju,2017-11-21 00:02:39.867000000,2017-11-20 00:02:39.867000000,20171120060038
但我应该在下面给出以下请求帮助
1,Nagraj,K,2017-11-21 00:02:39.867000000,2017-11-20 00:02:39.867000000,20171120060038 3,Ramya,Govindaraju,2017-11-21 00:02:39.867000000,2017-11-20 00:02:39.867000000,20171120060038
答案 0 :(得分:1)
我假设有一个名称字段包含','
ydata=[(1,'Nagraj,Keshav','2017-11-20 00:02:39.867000000','2017-11-20 00:02:39.867000000',20171120060038),(2,'Raghu,HR','2017-11-20 00:02:39.867000000','2017-11-20 00:02:39.867000000',20171120060038)]
yschema=['id','name','fdate','tdate','stamp']
tdata=[(1,'Nagraj,K','2017-11-21 00:02:39.867000000','2017-11-21 00:02:39.867000000',20171120060038),(2,'Raghu,HR','2017-11-21 00:02:39.867000000','2017-11-20 00:02:39.867000000',20171120060038),(3,'Ramya,Govindaraju','2017-11-21 00:02:39.867000000','2017-11-20 00:02:39.867000000',20171120060038)]
ydf=spark.createDataFrame(ydata,yschema)
tdf=spark.createDataFrame(tdata,yschema)
newdf=tdf.select('id','name').subtract(ydf.select('id','name'))
newdf.join(tdf,newdf['id']==tdf['id']).drop(tdf['id']).drop(tdf['name']).show()
输出:
| id| name| fdate| tdate| stamp|
+---+-----------------+--------------------+--------------------+--------------+
| 1| Nagraj,K|2017-11-21 00:02:...|2017-11-21 00:02:...|20171120060038|
| 3|Ramya,Govindaraju|2017-11-21 00:02:...|2017-11-20 00:02:...|20171120060038|