Found 507 Articles for Pandas

Write a program in Python to filter City column elements by removing the unique prefix in a given dataframe

Vani Nalliappan
Updated on 25-Feb-2021 07:16:20

254 Views

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

Write a program in Python Pandas to convert a dataframe Celsius data column into Fahrenheit

Vani Nalliappan
Updated on 25-Feb-2021 07:15:10

3K+ Views

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

Write a program to append Magic Numbers from 1 to 100 in a Pandas series

Vani Nalliappan
Updated on 25-Feb-2021 07:12:27

629 Views

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

Write a Python code to filter palindrome names in a given dataframe

Vani Nalliappan
Updated on 25-Feb-2021 07:09:49

515 Views

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

Write a program in Python to localize Asian timezone for a given dataframe

Vani Nalliappan
Updated on 25-Feb-2021 07:06:49

203 Views

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

Write a program to separate date and time from the datetime column in Python Pandas

Vani Nalliappan
Updated on 25-Feb-2021 07:05:47

9K+ Views

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

Write a program in Python to print numeric index array with sorted distinct values in a given series

Vani Nalliappan
Updated on 25-Feb-2021 07:01:19

74 Views

Assume, you have a series and the numberic index with sorted distinct values are −Sorted distict values - numeric array index [2 3 0 3 2 1 4] ['apple' 'kiwi' 'mango' 'orange' 'pomegranate']To solve this, we will follow the steps given below −SolutionApply pd.factorize() function inside list of non-unique elements and save it as index, index_value.index, unique_value = pd.factorize(['mango', 'orange', 'apple', 'orange', 'mango', 'kiwi', 'pomegranate'])Print the index and elements. Result is diplayed without sorting of distinct values and its indexApply pd.factorize() inside list elements and set sort=True then save it as sorted_index, unique_valuesorted_index, unique_value = pd.factorize(['mango', 'orange', 'apple', 'orange', 'mango', ... Read More

Write a program in Python to perform average of rolling window size 3 calculation in a given dataframe

Vani Nalliappan
Updated on 25-Feb-2021 07:00:07

146 Views

Assume, you have a dataframe and the result for rolling window size 3 calculation is, Average of rolling window is:    Id Age  Mark 0 NaN NaN  NaN 1 1.5 12.0 85.0 2 2.5 13.0 80.0 3 3.5 13.5 82.5 4 4.5 31.5 90.0 5 5.5 60.0 87.5To solve this, we will follow the below approach −SolutionDefine a dataframeApply df.rolling(window=2).mean() to calculate average of rolling window size 3 isdf.rolling(window=2).mean()ExampleLet’s check the following code to get a better understanding −import pandas as pd df = pd.DataFrame({"Id":[1, 2, 3, 4, 5, 6],                     ... Read More

Write a program in Python to slice substrings from each element in a given series

Vani Nalliappan
Updated on 25-Feb-2021 06:58:31

85 Views

Assume, you have a series and the result for slicing substrings from each element in series as, 0    Ap 1    Oa 2    Mn 3    KwTo solve this, we will follow the below approaches −Solution 1Define a seriesApply str.slice function inside start=0, stop-4 and step=2 to slice the substring from the series.data.str.slice(start=0, stop=4, step=2)ExampleLet’s check the following code to get a better understanding −import pandas as pd data = pd.Series(['Apple', 'Orange', 'Mango', 'Kiwis']) print(data.str.slice(start=0, stop=4, step=2))Output0    Ap 1    Oa 2    Mn 3    KwSolution 2Define a seriesApply string index slice to start from 0 ... Read More

Write a Python function to split the string based on delimiter and convert to series

Vani Nalliappan
Updated on 25-Feb-2021 06:55:36

540 Views

The result for splitting the string with ’' delimiter and convert to series as, 0    apple 1    orange 2    mango 3    kiwiTo solve this, we will follow the below approach −Solution 1define a function split_str() which accepts two arguments string and delimiterCreate s.split() function inside delimiter value and store it as split_datasplit_data = s.split(d)Apply split_data inside pd.Series() to generate series data.pd.Series(split_data)Finally, call the function to return the result.ExampleLet’s check the following code to get a better understanding −import pandas as pd def split_str(s, d):    split_data = s.split(d)    print(pd.Series(split_data)) split_str('apple\torange\tmango\tkiwi', '\t')Output0    apple 1   ... Read More

Advertisements