Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 765 of 855
Write a Python program to count the total number of ages between 20 to 30 in a DataFrame
Input −Assume, you have a DataFrame, Id Age 0 1 21 1 2 23 2 3 32 3 4 35 4 5 18Output −Total number of age between 20 to 30 is 2.SolutionTo solve this, we will follow the below approaches.Define a DataFrameSet the DataFrame Age column between 20,30. Store it in result DataFrame. It is defined below,df[df['Age'].between(20,30)]Finally, calculate the length of the result.ExampleLet us see the following implementation to get a better understanding.import pandas as pd data = {'Id':[1,2,3,4,5],'Age':[21,23,32,35,18]} df = pd.DataFrame(data) print(df) print("Count the age between 20 to 30") result = df[df['Age'].between(20,30)] print(len(result))Output Id Age 0 1 21 1 2 23 2 3 32 3 4 35 4 5 18 Count the age between 20 to 30 2
Read MoreWrite a program in Python to print the 'A' grade students' names from a DataFrame
Input −Assume, you have DataFrame, Id Name Grade 0 1 stud1 A 1 2 stud2 B 2 3 stud3 C 3 4 stud4 A 4 5 stud5 AOutput −And the result for ‘A’ grade students name, 0 stud1 3 stud4 4 stud5SolutionTo solve this, we will follow the below approaches.Define a DataFrameCompare the value to the DataFramedf[df['Grade']=='A']Store the result in another DataFrame and fetch Name.ExampleLet us see the following implementation to get a better understanding.import pandas as pd data = [[1, 'stud1', 'A'], [2, 'stud2', 'B'], [3, 'stud3', 'C'], [4, 'stud4', 'A'], ...
Read MoreWrite a program in Python to find the minimum age of an employee id and salary in a given DataFrame
Input −Assume, you have a DataFrameDataFrame is Id Age Salary 0 1 27 40000 1 2 22 25000 2 3 25 40000 3 4 23 35000 4 5 24 30000 5 6 32 30000 6 7 30 50000 7 8 28 20000 8 9 29 32000 9 10 27 23000Output −And, the result for a minimum age of an employee id and salary, Id Salary 1 2 25000SolutionTo solve this, we will follow the below approaches.Define a DataFrameSet ...
Read MoreWrite a program in Python to find the maximum length of a string in a given Series
Input −Assume, we have a Series like this, [“one”, “two”, “eleven”, “pomegranates”, “three”] and the maximum length of the string is “Pomegranates”SolutionTo solve this, we will follow the below approaches.Define a SeriesSet the initial value of a maxlen is 0Set the “maxstr” value is initially empty string.Create a for loop and access all the values in the Series one by one and create an if condition to compare the value based on the length as follows −for i in res: if(len(i)>maxlen): maxlen = len(i) maxstr = iFinally, print the value stored in the ...
Read MoreWrite a Python program to find the maximum value from first four rows in a given series
Input −Assume, you have a Series,0 11 1 12 2 66 3 24 4 80 5 40 6 28 7 50Output −Maximum value for first four row is 66.SolutionTo solve this, we will follow the steps given below −Define a SeriesSet rows value as data.iloc[0:4].Finally, find the max value from the rows series.ExampleLet us see the complete implementation to get a better understanding −import pandas as pd l = [11,12,66,24,80,40,28,50] data = pd.Series(l) rows = data.iloc[0:4] print(rows.max())Output66
Read MoreWrite a program in Python to round all the elements in a given series
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
Read MoreHow to print the days in a given month using Python?
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
Read MoreWrite a program in Python to print the day of the year in a given date series
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
Read MoreWrite a Python code to concatenate two Pandas series into a single series without repeating the index
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
Read MoreWrite 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
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}
Read More