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
Articles by Vani Nalliappan
Page 7 of 13
Write a program in Python to find the minimum rank of a particular column in a dataframe
SolutionAssume, you have a dataframe and minimum rank of a particular column, Id Name Age Rank 0 1 Adam 12 1.0 1 2 David 13 3.0 2 3 Michael 14 5.0 3 4 Peter 12 1.0 4 5 William 13 3.0To solve this, we will follow the steps given below −Define a dataframe.Assign df[‘Age’] column inside rank function to calculate the minimum rank for axis 0 is, df["Age"].rank(axis=0, method ='min', ascending=True)ExampleLet’s see the following code to get a better understanding −import pandas as pd data = {'Id': [1, 2, 3, ...
Read MoreWrite a program in Python to create a panel from a dictionary of dataframe and print the maximum value of the first column
The result for a maximum value of the first column in panel ismaximum value of first column is ; Column1 1.377292SolutionTo solve this, we will follow the below approach −Set data value as dictionary key is ‘Column1’ with value as pd.DataFrame(np.random.randn(5, 3))data = {'Column1' : pd.DataFrame(np.random.randn(5, 3))}Assign data to Panel and save it as pp = pd.Panel(data)Print the column using dict key Column1print(p['Column1'])Calculate the maximum value of first column using, minor_xs(0) ,p.minor_xs(0).max()ExampleLet’s see the following code to get a better understanding −import pandas as pd import numpy as np data = {'Column1' : pd.DataFrame(np.random.randn(5, 3))} p = pd.Panel(data) print("Panel ...
Read MoreWrite a program in Python to shift a dataframe index by two periods in positive and negative direction
Assume, you have a dataframe and the shift index by two periods in positive and negative direction is, shift the index by three periods in positive direction Id Age 2020-01-01 00:00:00 NaN NaN 2020-01-01 12:00:00 NaN NaN 2020-01-02 00:00:00 1.0 10.0 2020-01-02 12:00:00 2.0 12.0 2020-01-03 00:00:00 3.0 14.0 shift the index by three periods in negative direction Id Age 2020-01-01 00:00:00 3.0 14.0 2020-01-01 12:00:00 4.0 11.0 2020-01-02 00:00:00 5.0 13.0 2020-01-02 12:00:00 NaN NaN 2020-01-03 00:00:00 NaN NaNSolutionTo ...
Read MoreWrite a program in Python to remove first duplicate rows in a given dataframe
Assume, you have a dataframe and the result for removing first duplicate rows are, Id Age 0 1 12 3 4 13 4 5 14 5 6 12 6 2 13 7 7 16 8 3 14 9 9 15 10 10 14SolutionTo solve this, we will follow the steps given below −Define a dataframeApply drop_duplicates function inside Id and Age column then assign keep initial value as ‘last’.df.drop_duplicates(subset=['Id', 'Age'], keep='last')Store the result inside same dataframe and print itExampleLet’s see the below implementation to get a better understanding −import pandas ...
Read MoreWrite a program in Python to compute grouped data covariance and calculate grouped data covariance between two columns in a given dataframe
Assume, you have a dataframe and the result for calculating covariance from grouped data and corresponding column as, Grouped data covariance is: mark1 mark2 subjects maths mark1 25.0 12.500000 mark2 12.5 108.333333 science mark1 28.0 50.000000 mark2 50.0 233.333333 Grouped data covariance between two columns: subjects maths 12.5 science 50.0 dtype: float64SolutionTo solve this, we will follow the steps given below −Define a dataframeApply groupby function inside dataframe subjects ...
Read MoreWrite a program in Python to shift the first column and get the value from the user, if the input is divisible by both 3 and 5 and then fill the missing value
Input −Assume you have a DataFrame, and the result for shifting the first column and fill the missing values are, one two three 0 1 10 100 1 2 20 200 2 3 30 300 enter the value 15 one two three 0 15 1 10 1 15 2 20 2 15 3 30SolutionTo solve this, we will follow the below approach.Define a DataFrameShift the first column using below code, data.shift(periods=1, axis=1)Get the value from user and verify if it is divisible by 3 and 5. If the result is true then fill missing ...
Read MoreWrite a program to truncate a dataframe time series data based on index value
Assume you have a dataframe with time series data and the result for truncated data is, before truncate: Id time_series 0 1 2020-01-05 1 2 2020-01-12 2 3 2020-01-19 3 4 2020-01-26 4 5 2020-02-02 5 6 2020-02-09 6 7 2020-02-16 7 8 2020-02-23 8 9 2020-03-01 9 10 2020-03-08 after truncate: Id time_series 1 2 2020-01-12SolutionTo solve this, we will follow the steps given below −Define a dataframe.Create date_range function inside start=’01/01/2020’, periods = 10 and assign freq = ‘W’. It will generate ten dates from given start date to next weekly start dates and store it as df[‘time_series’].df['time_series'] ...
Read MoreWrite a program in Python to compute autocorrelation between series and number of lags
Assume, you have series and the result for autocorrelation with lag 2 is, Series is: 0 2.0 1 10.0 2 3.0 3 4.0 4 9.0 5 10.0 6 2.0 7 NaN 8 3.0 dtype: float64 series correlation: -0.4711538461538461 series correlation with lags: -0.2933396642805515SolutionTo solve this, we will follow the steps given below −Define a seriesFind the series autocorrelation using the below method, series.autocorr()Calculate the autocorrelation with lag=2 as follows, series.autocorr(lag=2)ExampleLet’s see the below code to get a better understanding, import pandas as pd import numpy as np series = ...
Read MoreWrite a program in Python to export a given dataframe into Pickle file format and read the content from the Pickle file
Assume you have a dataframe and the result for exporting into pickle file and read the contents from file as, Export to pickle file: Read contents from pickle file: Fruits City 0 Apple Shimla 1 Orange Sydney 2 Mango Lucknow 3 Kiwi WellingtonSolutionTo solve this, we will follow the steps given below −Define a dataframe.Export the dataframe to pickle format and name it as ‘pandas.pickle’, df.to_pickle('pandas.pickle')Read the contents from ‘pandas.pickle’ file and store it as result, result = pd.read_pickle('pandas.pickle')ExampleLet’s see the below implementation to get better understanding, import pandas as pd df = pd.DataFrame({'Fruits': ...
Read MoreWrite a program in Python to resample a given time series data and find the maximum month-end frequency
Assume, you have time series and the result for maximum month-end frequency, DataFrame is: Id time_series 0 1 2020-01-05 1 2 2020-01-12 2 3 2020-01-19 3 4 2020-01-26 4 5 2020-02-02 5 6 2020-02-09 6 7 2020-02-16 7 8 2020-02-23 8 9 2020-03-01 9 10 2020-03-08 Maximum month end frequency: Id time_series time_series 2020-01-31 4 2020-01-26 2020-02-29 8 2020-02-23 2020-03-31 10 2020-03-08SolutionTo solve this, we will follow the steps given below −Define a dataframe with one column, d = {'Id': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} ...
Read More