The picture of first 5 rows of my dataset is given below. What I want to do is that I want to create a new column called "Parking Type" and assign the values of the column as "Meter", "Ticket" and "Other" based on another column named "Sign". The "Sign" column is string which has some string values that have MTR and some that have TKT and some that have neither in them. So I just want to put inside "Parking Type" column the values "Meter" if a row in "Sign" column contains the string "MTR" in it and so on. I was doing something like this:
pSignInfringe['Parking Type'] = pSignInfringe.Sign.apply(lambda x: "Meter" if x == "1P MTR M-SAT 7:30-19:30" or x == "1/2P MTR SAT 7:30-1930" else "Ticket")
but then it will need too many or statements. Is there any better way to do that? I am new to python so sorry if it is a beginner question. The dataframe code is given below:
,Area Name,Street Name,Between Street 1,Between Street 2,Side Of Street,Street Marker,Arrival Time,Departure Time,Duration of Parking Event (in seconds),Sign,In Violation?,Street ID,Device ID,Month Number
8,City Square,FLINDERS STREET,SWANSTON STREET,RUSSELL STREET,3,1630N,2012-05-19 18:20:01,2012-05-19 19:19:58,3597,1/2P MTR SAT 7:30-1930,1,670,1123,5
10,Chinatown,RUSSELL STREET,Lt BOURKE STREET,BOURKE STREET,2,770E,2012-02-25 18:30:31,2012-02-25 21:02:36,9125,2P DIS M-SUN 0:00-23:59,1,1221,504,2
11,Princes Theatre,LONSDALE STREET,RUSSELL STREET,EXHIBITION STREET,1,C2858,2011-11-17 09:00:00,2011-11-17 10:41:06,6066,1P MTR M-SAT 7:30-19:30,1,894,1996,11
15,Southbank,COVENTRY STREET,DODDS STREET,WELLS STREET,4,9317S,2012-02-20 13:50:40,2012-02-20 16:33:33,9773,2P TKT A M-F 7:30-18:30,1,547,4054,2
28,Queensberry,VICTORIA STREET,KING STREET,HAWKE STREET,3,7642N,2012-02-15 11:32:34,2012-02-15 12:09:35,2221,1/4P M-SAT 7:30-18:30,1,1381,4001,2
30,Rialto,COLLINS STREET,KING STREET,WILLIAM STREET,3,2066N,2012-09-03 09:24:51,2012-09-03 10:45:41,4850,1/2P M-SAT 7:30-19:30,1,528,1290,9
45,Victoria Market,FRANKLIN STREET,QUEEN STREET,ELIZABETH STREET,1,C6628,2011-11-11 17:42:32,2011-11-11 19:50:44,7692,2P MTR M-SAT 7:30-20:30,1,681,2812,11
53,Hardware,LONSDALE STREET,QUEEN STREET,ELIZABETH STREET,1,C2942,2012-05-05 13:17:55,2012-05-05 14:59:35,6100,1P MTR M-SAT 7:30-19:30,1,894,2019,5
55,Hyatt,EXHIBITION STREET,Lt COLLINS STREET,COLLINS STREET,1,C364,2011-01-11 08:11:48,2011-01-11 16:48:39,31011,1P MTR M-SAT 7:30-19:30,1,647,243,1
56,Banks,QUEEN STREET,FLINDERS LANE,FLINDERS STREET,5,975W,2012-03-03 12:53:27,2012-03-03 14:06:27,4380,1P MTR M-SAT 7:30-19:30,1,1171,693,3
If the desired value of "ParkingType
depends only on the presence of "MTR", you may find this better. This will account for all cases where MTR is in the .Sign
field, without having to hard-code all possible values.
pSignInfringe['Parking Type'] = pSignInfringe.Sign.apply(lambda x: "Meter" if 'MTR' in x else "Ticket")