Тёмный
DEwithDhairy
DEwithDhairy
DEwithDhairy
Подписаться
Hi, Dhiraj Gupta here. Welcome to my channel to learn Data Engineering.
On This Channel We are covering interview question of various topics like DSA , PySpark , SQL and Pandas.
Stay Tuned for more Interview problems for the Data Engineer , Data Analyst and Data Science Role.
Комментарии
@ChandanDeveloper
@ChandanDeveloper 9 часов назад
when I try to install spark in windows home then getting error
@prajju8114
@prajju8114 2 дня назад
student_final=student_df.join(student_perc,'student_id') student_final.show() student_final=student_final.withColumn('Result',when(col('percentage')>=70,'Distinction').when((col('percentage')>=60)&(col('percentage')<=69),'First Class').when((col('percentage')>=50)&(col('percentage')<=59),'Second Class').when((col('percentage')>=40)&(col('percentage')<=49),'Third Class').otherwise('Fail')) student_final.show()
@prajju8114
@prajju8114 2 дня назад
from pyspark.sql.functions import * student_perc=marks_df.groupBy('student_id').agg((sum('marks')/count('student_id')).alias('percentage').cast('int')) student_perc.show()
@rawat7203
@rawat7203 2 дня назад
winner_df = battle_df.withColumn('winner_king', when(col('attacker_outcome') == 1, col('attacker_king')).otherwise(col('defender_king'))) joinDf = winner_df.join(king_df, winner_df.winner_king == king_df.k_no).select('region', 'house', 'battle_number') resultDf = joinDf.groupBy('region', 'house').agg(count('*').alias('battles_won_count')).withColumn('rn', rank().over(Window.partitionBy('region').orderBy(col('battles_won_count').desc()))).filter(col('rn')==1).drop('rn') resultDf.show()
@narravularushitha6806
@narravularushitha6806 6 дней назад
Hi, When am trying run sample code, Getting below error. Please help C:\Users ushi\Pyspark_Demo\venv\Scripts\python.exe C:\Users ushi\Pyspark_Demo\Test.py Traceback (most recent call last): File "C:\Users ushi\Pyspark_Demo\Test.py", line 1, in <module> from pyspark.sql import SparkSession File "C:\spark\spark-3.4.2-bin-hadoop3\python\pyspark\__init__.py", line 58, in <module> from pyspark.conf import SparkConf File "C:\spark\spark-3.4.2-bin-hadoop3\python\pyspark\conf.py", line 23, in <module> from py4j.java_gateway import JVMView, JavaObject ModuleNotFoundError: No module named 'py4j' Process finished with exit code 1
@talamanchisudheernadh698
@talamanchisudheernadh698 6 дней назад
from pyspark.sql import Window from pyspark.sql.functions import * df1=df.withColumn('rn',lead('flag').over(Window.partitionBy('emp_id').orderBy('log_date')))\ .withColumn('enddate',lead('log_date').over(Window.partitionBy('emp_id').orderBy('log_date')))\ .filter((col('rn')=='Y')&(col('flag')=='Y')).select('emp_id','log_date','enddate') df2=df1.union(df1).distinct() df3=df2.withColumn('daysk', dayofmonth(to_date(col('log_date'), 'dd-MM-yyyy')))\ .withColumn('rn',row_number().over(Window.partitionBy('emp_id').orderBy('daysk')))\ .withColumn('diff',col('daysk')-col('rn'))\ .groupBy('emp_id','diff').agg(min('log_date'),max('log_date')).show()
@talamanchisudheernadh698
@talamanchisudheernadh698 6 дней назад
from pyspark.sql import Window from pyspark.sql.functions import * df.withColumn('rn',lead('flag').over(Window.partitionBy('emp_id').orderBy('log_date')))\ .withColumn('rn1',lead('log_date').over(Window.partitionBy('emp_id').orderBy('log_date')))\ .filter((col('rn')=='Y')&(col('flag')=='Y')).show() +------+----------+----+---+----------+ |emp_id| log_date|flag| rn| rn1| +------+----------+----+---+----------+ | 102|02-01-2024| Y| Y|03-01-2024| | 102|05-01-2024| Y| Y|06-01-2024| | 102|06-01-2024| Y| Y|07-01-2024| | 103|05-01-2024| Y| Y|06-01-2024| +------+----------+----+---+----------+
@nsreeabburi2292
@nsreeabburi2292 6 дней назад
Thanks Diraj. Am trying to do via notebook when am execting the code am getting py4JJavaerror. And how can I see pyspark kernel in notebook do u have any idea about it
@rawat7203
@rawat7203 7 дней назад
Sir My way: joinedDf = friends_df.join(likes_df, [friends_df.friend_id == likes_df.user_id]).select(friends_df.user_id, 'friend_id', col('page_id').alias('fpage_id')) friendsLikeDf = joinedDf.groupBy('user_id').agg(collect_set('fpage_id').alias('friends_likes')) selfLikeDf = likes_df.groupBy('user_id').agg(collect_set('page_id').alias('self_likes')) allDf = friendsLikeDf.join(selfLikeDf,'user_id').select(friendsLikeDf.user_id,'friends_likes','self_likes') exceptDf = allDf.withColumn('test', array_except('friends_likes', 'self_likes')) resultDf = exceptDf.select('user_id', explode('test').alias('page_id')).orderBy('user_id')
@rajas6248
@rajas6248 9 дней назад
Lead and lag will helps to solve easily?
@rachitahuja1382
@rachitahuja1382 10 дней назад
likes_df_renamed = likes_df.select(col("user_id").alias("l_user_id"), col("page_id")) df_joined = friends_df.join(likes_df_renamed, on = friends_df.friend_id == likes_df_renamed.l_user_id , how= "inner") df_joined = df_joined.select(col("user_id"),col("page_id")).distinct() df_result = df_joined.join( likes_df, on=["user_id", "page_id"], how="left_anti" )
@rachitahuja1382
@rachitahuja1382 10 дней назад
# Create a window specification to partition by the row_id window_spec = Window.orderBy("row_id").rowsBetween(Window.unboundedPreceding, 0) # Fill null values with the last non-null value in the window job_skills_df = job_skills_df.withColumn("job_role", F.last("job_role", ignorenulls=True).over(window_spec))
@AnishNandyrevealed
@AnishNandyrevealed 18 дней назад
Why can't we use self join instead of window functions to solve this?
@user-gq6cg3ls7f
@user-gq6cg3ls7f 21 день назад
with cte as( select year(created_at) year, month(created_at) month, sum(valuess) total_sales from amazon_monthly_rev group by year(created_at), month(created_at) ) select concat(year, '-', '0',month) year_month , format(100*cast(total_sales - lag(total_sales) over (order by month)as decimal) / lag(total_sales) over (order by month),'##.##') pct from cte
@natarajbeelagi569
@natarajbeelagi569 24 дня назад
Can combine all steps into single step #importing functions from pyspark.sql.functions import * from pyspark.sql.window import Window as W from pyspark.sql.types import * window_spec = W.partitionBy(col("emp_id")).orderBy("log_date") df2 = ( df.filter(col("flag")== 'Y') .withColumn("log_date", to_date(col("log_date"),'dd-mm-yyyy')) .withColumn("rnk", row_number().over(window_spec)) .withColumn("Day", day("log_date")) .withColumn("day_diff", lit(col("Day")-col("rnk"))) .groupBy("emp_id", "day_diff").agg( count(lit(1)).alias("Consecutive_days"), min(col("log_date")).alias("Start_Date"), max(col("log_date")).alias("End_Date") ) .filter("Consecutive_days >=2") .drop("day_diff") ) #df2.show()
@Aishwarya-w8d
@Aishwarya-w8d 28 дней назад
finaldf = (sdf.withColumn('fn',coalesce(sdf.split_names.getItem(0),lit(None))) .withColumn('mn',coalesce(sdf.split_names.getItem(1),lit(None))) .withColumn('ln',coalesce(sdf.split_names.getItem(2),lit(None))) ).show()
@seansupp2315
@seansupp2315 29 дней назад
Hi Dhairy! I know you are incredibly busy and you probably get a lot questions, so this is going to take you 5 mins max. Could you please help me why my code is wrong? Your answer is going to make my day, if not weeks. My code: ------------------------------------------------------------ WITH maintable AS ( SELECT user_id, action, CAST(timestamp AS date) AS date, timestamp FROM facebook_web_log WHERE action in ('page_load','page_exit') ), page_load AS ( SELECT user_id, date, timestamp, ROW_NUMBER() OVER(PARTITION BY user_id, date ORDER BY timestamp DESC) AS rnk_load FROM maintable WHERE action = 'page_load' ), page_exit AS ( SELECT user_id, date, timestamp, ROW_NUMBER() OVER(PARTITION BY user_id, date ORDER BY timestamp) AS rnk_exit FROM maintable WHERE action = 'page_exit' ) SELECT *, e.timestamp - l.timestamp FROM page_load l INNER JOIN page_exit e ON l.user_id = e.user_id AND l.date = e.date AND l.rnk_load = 1 AND e.rnk_exit = 1 -------------------------------------------------------------------------------- When calculating for the differences in time, it seems like StrataScratch miscalculated 'only' for user 0. Could you please tell me why that is the case? I don't see flaws in my logic.
@VikashKumar-jx4qx
@VikashKumar-jx4qx Месяц назад
Hey, I want to know, is there any platform like hackerrank or leetcode for PySpark. Where I can practice PySpark.
@vivekdutta7131
@vivekdutta7131 Месяц назад
with credit AS ( select user_id,user_name, isnull((credit + amount),credit) as tot_credit from users u left join Transactions t on u.user_id = t.paid_to ) select user_id,user_name, isnull((tot_credit - amount),tot_credit) as final_credit, case when isnull((tot_credit - amount),tot_credit) > 0 then 'NO' else 'Yes' end as credit_limit_breached from credit u left join Transactions t on u.user_id = t.paid_by
@satirthahalder9528
@satirthahalder9528 Месяц назад
with cte as (select *,lag(log_date,1,log_date) over(partition by emp_id order by log_date) prev_date from t where flag ='Y'), cte2 as (select *,case when log_date-prev_date<2 then 0 else 1 end gp from cte), cte3 as (select *,sum(gp) over(partition by emp_id order by log_date) as gps from cte2) select emp_id, min(log_date)start_date,max(log_date)end_date,count(*) consecutive_days from cte3 group by emp_id, gps having count(*)>=2
@user-gq6cg3ls7f
@user-gq6cg3ls7f Месяц назад
another approach with success as( select *, DATEADD(day, -1*ROW_NUMBER() over (order by success_date), success_date) s_bucket, ROW_NUMBER() over (order by success_date) S_RN from succeeded where year(success_date) != '2018' ), s_final as( select min(success_date) start_date, max(success_date) end_date, ROW_NUMBER() over (order by (select null)) RN from success group by s_bucket ), fail as( select *, DATEADD(day, -1*ROW_NUMBER() over (order by fail_date), fail_date) f_bucket, ROW_NUMBER() over (order by fail_date) F_RN from failed where year(fail_date) != '2018' ), f_final as( select min(fail_date) start_date, max(fail_date) end_date, ROW_NUMBER() over (order by (select null)) RN from fail group by f_bucket ) select 'Succeeded' as period_date, format(start_date, 'dd-MM-yyyy') as start_date, format(end_date,'dd-MM-yyyy') as end_date from s_final union all select 'failed' as period_date, format(start_date,'dd-MM-yyyy') as start_date, format(end_date,'dd-MM-yyyy') as end_date from f_final order by start_date
@user-gq6cg3ls7f
@user-gq6cg3ls7f Месяц назад
another approach using SQL Server with U1 as( select User1, count(*) cnt from facebook group by User1 ), U2 as( select User2, count(*) cnt from facebook group by User2 ),final as( select coalesce(User1, User2) as Users, (isnull(U1.cnt,0) + isnull(U2.cnt,0)) as Popularity_Percentage from U1 full join U2 on U1.User1=U2.User2 ) select Users, round(cast(round(Popularity_Percentage,2) as decimal)*100/ count(user) over (order by user rows between unbounded preceding and unbounded following),3) total_users from final
@nagumallasravansai249
@nagumallasravansai249 Месяц назад
you are awesome buddy!
@Tech_world-bq3mw
@Tech_world-bq3mw Месяц назад
my solution: #df- original, and df1-with range numbers df_missing=df1.join(df, df1.id==df.id, how='left_anti') df_missing.show()
@vivekdutta7131
@vivekdutta7131 Месяц назад
with cte AS ( select question_id, isnull(sum(case when action = 'show' then 1 end),0) as show_count, isnull(sum(case when action = 'answer' then 1 end),0) as answer_count from survey_log group by question_id ),ctf as ( select question_id,(answer_count/show_count) as answer_rate, row_number() over(order by (answer_count/show_count) desc ,question_id asc) as rn from cte ) select question_id from ctf where rn = 1
@torrentdownloada
@torrentdownloada Месяц назад
Hello, i think you might have missed this scenario, what if the id's are some random integers or some strings. Please review my solution and let me know if my understanding is correct. Thanks with cte as (select *,row_number() over() rn from swap_id) select name,id,case when a%2=0 then lag(id) over() when a%2<>0 then coalesce(lead(id) over(),id) else id end swapped from cte
@Hope-xb5jv
@Hope-xb5jv Месяц назад
with cte as ( select question_id, sum(case when action = 'answer' then 1 else 0 end)/sum(case when action = 'show' then 1 else 0 end) as answer_rate from survey_log group by question_id ),cte2 as ( select question_id ,row_number()over(order by question_id ) as id, dense_rank()over(order by answer_rate desc) as rate from cte ) select question_id from cte2 where id = 1 and rate = 1
@Aman-lv2ee
@Aman-lv2ee Месяц назад
select name, case when id%2 =0 then lag(id,1,id)over(order by id) when id%2 !=0 then lead(id,1,id)over(order by id) end as swap_ids from swap_id;
@AishwaryaAishwarya-e4l
@AishwaryaAishwarya-e4l Месяц назад
Thankyouuuuuuuuuu
@DEwithDhairy
@DEwithDhairy Месяц назад
Glad u found useful
@rahulmittal116
@rahulmittal116 Месяц назад
Nice🤙
@DEwithDhairy
@DEwithDhairy Месяц назад
Thanks Rahul
@Ak12345-g
@Ak12345-g Месяц назад
with cte as( select t2name,STRING_AGG(topping_name,',') as pizza,sum(cost) as total_cost from( select t1.*,t2.topping_name as t2name,t2.cost as t2cost from toppings t1 cross join toppings t2 where t1.topping_name <> t2.topping_name)a group by t2name) select pizza,total_cost from cte order by total_cost;
@DEwithDhairy
@DEwithDhairy Месяц назад
Great approach.
@Ak12345-g
@Ak12345-g Месяц назад
@@DEwithDhairy Thank you so much 🥰
@Aman-lv2ee
@Aman-lv2ee Месяц назад
nice video, share more on python questsion for DE, thanks
@DEwithDhairy
@DEwithDhairy Месяц назад
Thabks, Its in the pipeline
@vivekdutta7131
@vivekdutta7131 Месяц назад
with cte AS ( select * , row_number() over(order by topping_name) as rn from toppings ),ctf AS ( select t.topping_name as topping_name1,g.topping_name as topping_name2,t.cost as cost from cte t , cte g where 1 = 1 and t.rn <> g.rn ) select string_agg(topping_name1,',') as pizza,sum(cost) as total_cost from ctf group by topping_name2
@user-gq6cg3ls7f
@user-gq6cg3ls7f Месяц назад
select concat(z.topping_name, ',' , y.topping_name, ',', x.topping_name) as pizza, x.cost+y.cost+z.cost as total_cost from toppings x inner join toppings y on x.topping_name > y.topping_name inner join toppings z on y.topping_name > z.topping_name
@DEwithDhairy
@DEwithDhairy Месяц назад
Correction At 4:16 : Self Join *
@nikunjmistry373
@nikunjmistry373 Месяц назад
Thank you, Dhiraj, for using my website and giving a shoutout! I'm really happy to see my website being used by RU-vid content creators.😊
@vrishabhbhonde6899
@vrishabhbhonde6899 Месяц назад
@nikunjmistry373 Sir aapne same python ke questions solve krei hai kya Leetcode or Statascractch ke ? Is there any website like this for python questions that you've made? Thank you for SQL website. Its helpful
@cretive549
@cretive549 Месяц назад
Sir me clg student hu mujhe data engineer banna h but samaj nhi aa raha h ki kya kya padhu or kaha se padhu. Mujhe python and sql aata h bus
@DEwithDhairy
@DEwithDhairy Месяц назад
SQL and python me master bano, Cloud sikh lo koi
@cretive549
@cretive549 Месяц назад
@@DEwithDhairy sir koi bolta h pyspark sikhena padega or bhi kuch and cloud me kya kya sikhna h please sir guide me
@nikunjmistry373
@nikunjmistry373 Месяц назад
@@cretive549 I would suggest something. Don't go what all r doing mern and full stack. For DE do etl script play with docker apache spark handle data in apache supaset... step by step . For any fresher first thing is sql and python . Rest as needed..
@CharanSaiAnnam
@CharanSaiAnnam Месяц назад
my solution: @udf def removeSpaceandVowels(r): s = "" for i in r: if i not in "aeiou ": s += i return s dfr = df.withColumn("consonants" , removeSpaceandVowels(col("message")))\ .drop("message").withColumn("consonantCount" , length(col("consonants")))
@satishgs5355
@satishgs5355 Месяц назад
from pyspark.sql.window import Window df1 = df.withColumn("rnk",row_number().over(Window.partitionBy(df.emp_id).orderBy(col("emp_id")))).filter(col("rnk")>1) df1.show() df2 = df1.dropDuplicates(df.columns) df2.show()
@DEwithDhairy
@DEwithDhairy Месяц назад
👏
@KeshannaKummari-y6h
@KeshannaKummari-y6h Месяц назад
l=[1,2,3,[4,5],[6,7],[7,8,9]] m=[] for i in l: if type(i)==int: m.append(i) elif type(i)==list: for j in range(len(i)): m.append(i[j]) else: pass print(m)
@DEwithDhairy
@DEwithDhairy Месяц назад
Hi, This solution will not work if we have nesting in the list like this [[[2, 3]], 10]
@gauravpathak9625
@gauravpathak9625 Месяц назад
from pyspark.sql.window import Window from pyspark.sql.functions import col,row_number w1 = Window.partitionBy(col("cust_id")).orderBy(col("flight_id")) w2 = Window.partitionBy(col("cust_id")).orderBy(col("flight_id").desc()) df_origin = df_flight.withColumn("rank_asc", row_number().over(w1)).filter((col("rank_asc") == 1)).withColumnRenamed("origin","origin_1").withColumnRenamed("cust_id","cust_id_o") df_destination = df_flight.withColumn("rank_desc", row_number().over(w2)).filter((col("rank_desc") == 1)).withColumnRenamed("destination","destination_1") df_final = df_origin.join(df_destination, df_origin.cust_id_o == df_destination.cust_id, "inner").select(col("cust_id"),col("origin_1").alias("origin"),col("destination_1").alias("destination")) display(df_final)
@KapilKumar-hk9xk
@KapilKumar-hk9xk Месяц назад
Amazing <3
@DEwithDhairy
@DEwithDhairy Месяц назад
Thanks 😄
@nupoornawathey100
@nupoornawathey100 Месяц назад
Easier solution- Scala Spark using DF API- val common_likes_df = friends_df.as("f").join(likes_df.alias("l"), $"f.friend_id" === $"l.user_id", "inner" ).select("f.user_id", "l.page_id") common_likes_df.as("cl").join(likes_df.as("l"), $"cl.user_id" === $"l.user_id" && $"cl.page_id" === $"l.page_id", "left_anti").orderBy($"user_id").show(false) Using Spark SQL- spark.sql(""" with common_likes as ( select f.user_id, l.page_id from friends f inner join likes l on f.friend_id=l.user_id ) select user_id, page_id from common_likes where (user_id, page_id) not in (select user_id, page_id from likes) order by user_id """).show(false) Output: +-------+-------+ |user_id|page_id| +-------+-------+ |2 |B | |2 |C | |3 |A | |4 |C | |4 |C | |4 |A | +-------+-------+
@Chathur732
@Chathur732 Месяц назад
with cte as ( select *, extract (day from P1_date) - row_number() over(order by (select null)) as diff2 from ( select *, case when extract (day from ld) - extract (day from P1_date) = 1 then 1 end as diff from ( select P1.emp_id as P1_emp_id, P1.log_date as P1_date, P1.flag as P1_flag, lead(P1.log_date) over(partition by P1.emp_id order by P1.log_date) as ld from pwc_attandance_log P1 where P1.flag = 'Y') a where case when extract (day from ld) - extract (day from P1_date) = 1 then 1 end is not null) b) select p1_emp_id, min(p1_date), max(ld), count(diff2) + 1 as no_of_days from cte group by p1_emp_id, diff2
@kalaivanik8872
@kalaivanik8872 Месяц назад
mysql with per_month as (select year(created_at) as yr,month(created_at) as mon,sum(value)total_per_mon from amazon_monthly_rev group by year(created_at),month(created_at)) ,prev_revenue as (select *,lag(total_per_mon,1,0) over()pre from per_month) select concat(yr,'-',mon)as yearMonth,round((total_per_mon-pre)/pre*100,2)as rev_diff from prev_revenue;
@kalaivanik8872
@kalaivanik8872 Месяц назад
with percentage as (select student_id,round(sum(marks)/count(1),0) as percentage from marks group by student_id) select s.student_id,s.name,p.percentage, case when p.percentage >=70 then 'Distinction' when p.percentage between 60 and 69 then 'First Class' when p.percentage between 50 and 59 then 'Second Class' when p.percentage between 40 and 49 then 'Thid Class' when p.percentage <= 39 then 'Fail' end as Result from student s join percentage p on s.student_id = p.student_id;
@kalaivanik8872
@kalaivanik8872 Месяц назад
with first_joindate as (select *,min(join_date) over(partition by user_id)as first from user_data) ,new_user_count as ( select join_date, sum(case when join_date = first then 1 else 0 end)new_user,count(1)total_user from first_joindate group by join_date) select join_date,new_user,case when new_user > 0 then round((new_user/total_user) * 100,0) else 0 end as percentage_new_user from new_user_count;
@kalaivanik8872
@kalaivanik8872 Месяц назад
with cte as (select *, (total_sales_revenue-lag(total_sales_revenue,1,0) over(partition by product_id))diff from salesq) select * from products where product_id = (select product_id from cte group by product_id having min(diff) > 0)
@kalaivanik8872
@kalaivanik8872 Месяц назад
create table pop(user1 int,user2 int); insert into pop values (1,5), (1,3), (1,6), (2,1), (2,6), (3,9), (4,1), (7,2), (8,3); with all_pairs as( select user1,user2 from pop union select user2,user1 from pop) select user1 as user,round(count(user1)/(select count(distinct user1) from all_pairs)*100,2) as per from all_pairs group by user1;
@kalaivanik8872
@kalaivanik8872 Месяц назад
select a.source,a.destination from flight a join flight b on a.source < b.source and a.destination = b.source;