Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
bus_fare_df = bus_fare_df.sort_values(by = ["Bus","Service_Date","RecordedAt" ])
# display(bus_fare_df.head())
test = bus_fare_df[["Bus","Service_Date","RecordedAt","Seat_Fare_Type_1_average" ]].sort_values(by = ["Bus","Service_Date","RecordedAt" ])
test = test[["Bus","Service_Date","Seat_Fare_Type_1_average" ]]
test["Seat_Fare_Type_1_average_impute"] = test.groupby(["Bus","Service_Date" ]).transform(lambda x: x.replace(to_replace=0, method='ffill'))
display(test.shape)
display(bus_fare_df.shape)
test2 = bus_fare_df[["Bus","Service_Date","RecordedAt","Seat_Fare_Type_2_average" ]].sort_values(by = ["Bus","Service_Date","RecordedAt" ])
test2 = test2[["Bus","Service_Date","Seat_Fare_Type_2_average" ]]
test2["Seat_Fare_Type_2_average_impute"] = test2.groupby(["Bus","Service_Date" ]).transform(lambda x: x.replace(to_replace=0, method='ffill'))
display(test2.shape)
# display(bus_fare_df.shape)
bus_fare_df["Seat_Fare_Type_1_average_impute_ffil"] = test["Seat_Fare_Type_1_average_impute"]
bus_fare_df["Seat_Fare_Type_2_average_impute_ffil"] = test2["Seat_Fare_Type_2_average_impute"]
# display(bus_fare_df.head())
#############################################################################################################################
test = bus_fare_df[["Bus","Service_Date","RecordedAt","Seat_Fare_Type_1_average" ]].sort_values(by = ["Bus","Service_Date","RecordedAt" ])
test = test[["Bus","Service_Date","Seat_Fare_Type_1_average" ]]
test["Seat_Fare_Type_1_average_impute_bfil"] = test.groupby(["Bus","Service_Date" ]).transform(lambda x: x.replace(to_replace=0, method='bfill'))
display(test.shape)
test2 = bus_fare_df[["Bus","Service_Date","RecordedAt","Seat_Fare_Type_2_average" ]].sort_values(by = ["Bus","Service_Date","RecordedAt" ])
test2 = test2[["Bus","Service_Date","Seat_Fare_Type_2_average" ]]
test2["Seat_Fare_Type_2_average_impute_bfil"] = test2.groupby(["Bus","Service_Date" ]).transform(lambda x: x.replace(to_replace=0, method='bfill'))
display(test2.shape)
bus_fare_df["Seat_Fare_Type_1_average_impute_bfill"] = test["Seat_Fare_Type_1_average_impute_bfil"]
bus_fare_df["Seat_Fare_Type_2_average_impute_bfill"] = test2["Seat_Fare_Type_2_average_impute_bfil"]
# display(bus_fare_df.hea
#############################################################################################################################
test_a = bus_fare_df[["Bus","Service_Date","RecordedAt","average_price_s1_s2" ]].sort_values(by = ["Bus","Service_Date","RecordedAt" ])
test_a = test_a[["Bus","Service_Date","average_price_s1_s2" ]]
test_a["average_price_s1_s2_bfil"] = test_a.groupby(["Bus","Service_Date" ]).transform(lambda x: x.replace(to_replace=0, method='bfill'))
display(test_a.shape)
test_f = bus_fare_df[["Bus","Service_Date","RecordedAt","average_price_s1_s2" ]].sort_values(by = ["Bus","Service_Date","RecordedAt" ])
test_f = test_f[["Bus","Service_Date","average_price_s1_s2" ]]
test_f["average_price_s1_s2_ffil"] = test_f.groupby(["Bus","Service_Date" ]).transform(lambda x: x.replace(to_replace=0, method='ffill'))
display(test_f.shape)
bus_fare_df["average_price_s1_s2_bfill"] = test_a["average_price_s1_s2_bfil"]
bus_fare_df["average_price_s1_s2_ffill"] = test_f["average_price_s1_s2_ffil"]
# display(bus_fare_df.head())
#############################################################################################################################
bus_fare_df['average_price_s1_s2_filled'] = bus_fare_df.apply(lambda x: average_s1_s2_price(x.average_price_s1_s2_ffill, x.average_price_s1_s2_bfill), axis=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment