Assume, you have a dataframe and the result for a date, month, year column is, date day month year 0 17/05/2002 17 05 2002 1 16/02/1990 16 02 1990 2 25/09/1980 25 09 1980 3 11/05/2000 11 05 2000 4 17/09/1986 17 09 1986To solve this, we will follow the steps given below −SolutionCreate a list of dates and assign into dataframe.Apply str.split function inside ‘/’ delimiter to df[‘date’] column. Assign the result to df[[“day”, “month”, “year”]].ExampleLet’s check the following code to get a better understanding −import ... Read More
Assume, you have a series and the result for converting to dummy variable as, Female Male 0 0 1 1 1 0 2 0 1 3 1 0 4 0 1 5 0 0 6 1 0 7 1 0To solve this, we will follow the steps given below −SolutionCreate a list with ‘Male’ and ‘Female’ elements and assign into Series.Apply get_dummies function inside series and set dummy_na value as False. It is defined below, pd.get_dummies(series, dummy_na=False)ExampleLet’s check the following code to get ... Read More
Assume, you have a dataframe and the result for converted to latex as, \begin{tabular}{lrr} \toprule {} & Id & Age \ \midrule 0 & 1 & 12 \ 1 & 2 & 13 \ 2 & 3 & 14 \ 3 & 4 & 15 \ 4 & 5 & 16 \ \bottomrule \end{tabular}SolutionTo solve this, we will follow the steps given below −Define a dataframeApply to_latex() function to the dataframe and set index and multirow values as True. It is defined below, df.to_latex(index = True, multirow = True)ExampleLet’s ... Read More
The result for generating even length random four-digit pin numbers as, enter the series size 4 Random four digit pin number series 0 0813 1 7218 2 6739 3 8390To solve this, we will follow the steps given below −SolutionCreate an empty and list and set result as TrueSet while loop and get the size from the userSet if condition to find the size is even or odd. If the size is odd then assign the result as False and runs the loop until an even number is entered.l = [] while(True): size = int(input("enter ... Read More
Assume you have a dataframe, the result for removing unique prefix city names are, Id City 2 3 Kolkata 3 4 Hyderabad 6 7 Haryana 8 9 Kakinada 9 10 KochinTo solve this, we will follow the steps given below −SolutionDefine a dataframeCreate an empty list to append all the city column values first char’s, l = [] for x in df['City']: l.append(x[0])Create another empty list to filter repeated char.Set for loop and if condtion to append unique char’s. It is defined below, l1 = [] for j in l: if(l.count(j)>1): if(j not in ... Read More
The result for converting celsius to Fahrenheit as, Id Celsius Fahrenheit 0 1 37.5 99.5 1 2 36.0 96.8 2 3 40.0 104.0 3 4 38.5 101.3 4 5 39.0 102.2To solve this, we will follow below approaches −Solution 1Define a dataframe with ‘Id’ and ‘Celsius’ column valuesApply df.assign function inside write lambda function to convert celsius values by multiplying (9/5)*df[celsius]+32 and assign it to Fahrenheit. It is defined below −df.assign(Fahrenheit = lambda x: (9/5)*x['Celsius']+32)ExampleLet’s check the following code to get a better understanding −import pandas as pd df = pd.DataFrame({'Id':[1, 2, 3, 4, 5], ... Read More
The result for appending magic numbers from 1 to 100 is, magic number series: 0 1 1 10 2 19 3 28 4 37 5 46 6 55 7 64 8 73 9 82 10 91 11 100To solve this, we will follow the below approaches −Solution 1Create list comprehension to append 1 to 100 values to list ls.ls = [i for i in range(1, 101)]Apply ... Read More
Result for printing palindrome names are −Palindrome names are: Id Name 0 1 bob 2 3 hannahTo solve this, we will follow the below approaches −Solution 1Define a dataframeCreate list comprehension inside set for loop to access all the values from df[‘Name’] column using i variable and set if condition to compare i==i[::-1] then add i value to the listl = [ i for i in df['Name'] if(i==i[::-1])]Finally, check the list values present in the df[‘Name’] column using isin()df[df['Name'].isin(l)]ExampleLet’s check the following code to get a better understanding −import pandas as pd data = ... Read More
Assume, you have a time series and the result for localize asian time zone as, Index is: DatetimeIndex(['2020-01-05 00:30:00+05:30', '2020-01-12 00:30:00+05:30', '2020-01-19 00:30:00+05:30', '2020-01-26 00:30:00+05:30', '2020-02-02 00:30:00+05:30'], dtype='datetime64[ns, Asia/Calcutta]', freq='W-SUN')SolutionDefine a dataframeCreate time series using pd.date_range() function with start as ‘2020-01-01 00:30’, periods=5 and tz = ‘Asia/Calcutta’ then store it as time_index.time_index = pd.date_range('2020-01-01 00:30', periods = 5, freq ='W', tz = 'Asia/Calcutta')Set df.index to store localized time zone from time_indexdf.index = time_indexFinally print the localized timezoneExampleLet’s check the ... Read More
Assume, you have datetime column in dataframe and the result for separating date and time as, datetime date time 0 2020-01-01 07:00:00 2020-01-06 07:00:00 1 2020-01-02 07:00:00 2020-01-06 07:00:00 2 2020-01-03 07:00:00 2020-01-06 07:00:00 3 2020-01-04 07:00:00 2020-01-06 07:00:00 4 2020-01-05 07:00:00 2020-01-06 07:00:00 5 2020-01-06 07:00:00 2020-01-06 07:00:00To solve this, we will follow the below approaches −Solution 1Define a dataframe ‘datetime’ column using pd.date_range(). It is defined below, pd.DataFrame({'datetime':pd.date_range('2020-01-01 07:00', periods=6)})Set for loop d variable to access df[‘datetime’] column one by one.Convert date and time from for loop and save it as df[‘date’] ... Read More