Found 507 Articles for Pandas

Write a program in Python to round all the elements in a given series

Vani Nalliappan
Updated on 24-Feb-2021 06:57:19

56 Views

Input −Assume, you have a Series,0    1.3 1    2.6 2    3.9 3    4.8 4    5.6Output −0    1.0 1    3.0 2    4.0 3    5.0 4    6.0Solution 1Define a SeriesCreate an empty list. Set the for loop to iter the data. Append round of values to the list.Finally, add the elements to the series.ExampleLet us see the complete implementation to get a better understanding −import pandas as pd l = [1.3,2.6,3.9,4.8,5.6] data = pd.Series(l) print(data.round())Output0    1.0 1    3.0 2    4.0 3    5.0 4    6.0Solution 2Exampleimport pandas as pd l = [1.3,2.6,3.9,4.8,5.6] data = pd.Series(l) ls = [] for i,j in data.items():    ls.append(round(j)) result = pd.Series(ls) print(result)Output0    1 1    3 2    4 3    5 4    6

How to print the days in a given month using Python?

Vani Nalliappan
Updated on 24-Feb-2021 06:49:57

263 Views

Input −Assume, you have date series to find the number of days in a month.SolutionTo solve this, we will follow the steps given below −Define date seriesSet date_range value as 2020-02-10.find the number of days in a month using Series.dt.daysinmonthExampleLet us see the complete implementation to get a better understanding −import pandas as pd date = pd.date_range('2020-02-10',periods=1) data = pd.Series(date) print(data.dt.daysinmonth)Output0    29

Write a program in Python to print the day of the year in a given date series

Vani Nalliappan
Updated on 24-Feb-2021 06:49:11

218 Views

Input −Assume, you have a series and find the day of the year from a given specific range of dates.SolutionTo solve this, we will follow the below approaches.Define a SeriesSet date_range as ‘2020-01-10’ with five periods. It is defined below,pd.date_range('2020-01-10',periods=5)Find the day using Series.dt.dayofyear.ExampleLet us see the complete implementation to get a better understanding −import pandas as pd date = pd.date_range('2020-01-10',periods=5) data = pd.Series(date) print(data.dt.dayofyear)Output0    10 1    11 2    12 3    13 4    14

Write a Python code to concatenate two Pandas series into a single series without repeating the index

Vani Nalliappan
Updated on 24-Feb-2021 06:47:22

325 Views

Input − Assume, you have a series and the result to concat the values without repeating the index is,0    1 1    2 2    3 3    4 4    5 5    6SolutionTo solve this, we will follow these two steps −Define two SeriesConcat two series and apply ignore_index value as True to find the result. It is defined below,pd.concat([series_one,series_two],ignore_index=True)ExampleLet us see the complete implementation to get a better understanding −import pandas as pd series_one = pd.Series([1,2,3]) series_two = pd.Series([4,5,6]) print(pd.concat([series_one,series_two],ignore_index=True))Output0    1 1    2 2    3 3    4 4    5 5    6

Write a Python code to create a series with your range values, generate a new row as sum of all the values and then convert the series into json file

Vani Nalliappan
Updated on 24-Feb-2021 06:45:38

372 Views

SolutionTo solve this, we will follow the steps given below −Define a series with a range of 1 to 10Find the sum of all the valuesConvert the series into JSON file formatLet us see the following implementation to get a better understanding.Exampleimport pandas as pd data = pd.Series(range(1,11)) data['sum'] = data.sum() data = data.to_json() print(data)Output{"0":1,"1":2,"2":3,"3":4,"4":5,"5":6,"6":7,"7":8,"8":9,"9":10,"sum":55}

Write a program in Python to find the missing element in a given series and store the full elements in the same series

Vani Nalliappan
Updated on 24-Feb-2021 06:44:26

212 Views

SolutionTo solve this, we will follow the steps given below −Define a Series.Create a for loop and access the data from start to end elements. Set if condition to check the data is present or not.If the value is not in the range then append it to the list. Finally, sort and print the values.for i in range(data[0], data[length-1]):    if(i not in data):       l1.append(i)    else:       l1.append(i)ExampleLet us see the following implementation to get a better understanding.import pandas as pd import numpy as np l = [1, 2, 3, 6, 7] l1 = ... Read More

Write a program in Python to generate any random five prime numbers between 100 to 150 in a Series

Vani Nalliappan
Updated on 24-Feb-2021 06:41:08

778 Views

SolutionTo solve this, we will follow the steps given below −Define an empty listCreate a for loop and set range from 100 to 150Set another for loop to access the values from 2 to range of values and find the factors, if nothing is found then add to the list. It is defined below, for i in range(100, 150):    for j in range(2, i):       if(i % j == 0):          break    else:       l.append(i)Set random sample value as 5 and assign into the list then finally create a Series.data = ... Read More

Write a program in Python to find the index for NaN value in a given series

Vani Nalliappan
Updated on 24-Feb-2021 06:42:32

686 Views

Input −Assume, you have a series, 0    1.0 1    2.0 2    3.0 3    NaN 4    4.0 5    NaNOutput − And, the result for NaN index is, index is 3 index is 5SolutionTo solve this, we will follow the steps given below −Define a Series.Create for loop and access all the elements and set if condition to check isnan(). Finally print the index position. It is defined below, for i, j in data.items(): if(np.isnan(j)):    print("index is", i)ExampleLet us see the following implementation to get a better understanding.import pandas as pd import numpy as np l ... Read More

Write a program in Python to filter the elements in a series which contains a string start and endswith ‘a’

Vani Nalliappan
Updated on 24-Feb-2021 06:37:41

48 Views

Input − Assume, you have a Series, 0    apple 1    oranges 2    alpha 3    aroma 4    betaOutput − And, the result for elements start and endswith ‘a’.2    alpha 3    aromaSolution 1Define a Series.Create regular expression to check start and endswith ‘a’r'^[a]$|^([a]).*\1$'Create an empty list and set for loop and set if condition inside to check the pattern. It is defined below, for i in data:    if(re.search(exp, i)):       ls.append(i)Finally, check the series using isin().ExampleLet us see the following implementation to get a better understanding.import pandas as pd import re l ... Read More

Write a program in Python to print the power of all the elements in a given series

Vani Nalliappan
Updated on 24-Feb-2021 06:34:19

145 Views

Input − Assume, you have a series, 0    1 1    2 2    3 3    4Output − And, the result for the power of all elements in a series is, 0    1 1    4 2    27 3    256Solution 1Define a Series.Create transform method inside apply lambda power value. It is defined below, data.transform(lambda x:x**x)data.transform(lambda x:x**x)Solution 2Define a Series.Create an empty list. Create for loop, iter all the items. Append elements to the list. It is defined below, for i, j in data.items():    ls.append(m.pow(j, j))Finally, convert the list into Series.ExampleLet us see the ... Read More

Advertisements