0

This is the dataset of parking area. The dataset have different parking area with different time as well. I want to insert rate for specific parking area for different time like morning, evening and night. https://prnt.sc/5pl1usWXVZQt

I tried grouping according to area and time. Now I want to insert accordingly. for example parkingrate for FirstHill at morning 8 to 11AM is 0.5$. Can anyone help me with this? https://prnt.sc/iTohLXmkYXsd

df_parking_1 = df_parking.groupby(['PaidParkingArea','OccupancyDateTime'])
samita
  • 165
  • 1
  • 11

1 Answers1

0

You do not need Panda groupby to do that, here is a reproducible example:

import pandas as pd

df = pd.DataFrame(
    {
        "OccupancyDateTime": [
            "06/05/2023 04:12:00 PM",
            "06/05/2023 08:23:00 AM",
            "06/05/2023 09:15:00 AM",
            "06/05/2023 04:12:00 PM",
            "06/05/2023 10:58:00 AM",
        ],
        "PaidParkingArea": [
            "First Hill",
            "First Hill",
            "Commercial core",
            "Belltown",
            "First Hill",
        ],
    }
)

To insert a new column PaidParkingRate which value is $ 0.5 when PaidParkingArea is First Hill and OccupancyDateTime is beteween 8 AM and 11 AM, you can for instance use Pandas loc instead:

# Format column values
df["OccupancyDateTime"] = pd.to_datetime(
    df["OccupancyDateTime"], format="%d/%m/%Y %I:%M:%S %p"
)

# Set new values
df.loc[
    (df["OccupancyDateTime"].dt.hour >= 8)
    & (df["OccupancyDateTime"].dt.hour < 11)
    & (df["PaidParkingArea"] == "First Hill"),
    "PaidParkingRate",
] = 0.5

Then:

print(df)
# Output

    OccupancyDateTime  PaidParkingArea  PaidParkingRate
0 2023-05-06 16:12:00       First Hill              NaN
1 2023-05-06 08:23:00       First Hill              0.5
2 2023-05-06 09:15:00  Commercial core              NaN
3 2023-05-06 16:12:00         Belltown              NaN
4 2023-05-06 10:58:00       First Hill              0.5
Laurent
  • 12,287
  • 7
  • 21
  • 37