Found 507 Articles for Pandas

Write a program in Python to print the first and last three days from a given time series data

Vani Nalliappan
Updated on 25-Feb-2021 06:52:25

107 Views

Assume, you have time series and the result for the first and last three days from the given series as, first three days: 2020-01-01    Chennai 2020-01-03    Delhi Freq: 2D, dtype: object last three days: 2020-01-07    Pune 2020-01-09    Kolkata Freq: 2D, dtype: objectTo solve this, we will follow the steps given below −SolutionDefine a series and store it as data.Apply pd.date_range() function inside start date as ‘2020-01-01’ and periods = 5, freq =’2D’ and save it as time_seriestime_series = pd.date_range('2020-01-01', periods = 5, freq ='2D')Set date.index = time_seriesPrint the first three days using data.first(’3D’) and save it ... Read More

Write a program in Python to generate a random array of 30 elements from 1 to 100 and calculate maximum by minimum of each row in a dataframe

Vani Nalliappan
Updated on 25-Feb-2021 06:51:25

79 Views

Result for generating dataframe maximum by a minimum of each row is0    43.000000 1    1.911111 2    2.405405 3    20.000000 4    7.727273 5    6.333333To solve this, we will follow the steps given below −Solution 1Define a dataframe with size of 30 random elements from 1 to 100 and reshape the array by (6, 5) to change 2-D arraydf = pd.DataFrame(np.random.randint(1, 100, 30).reshape(6, 5))Create df.apply function inside lambda method to calculate np.max(x)/np.min(x) with axis as 1 and save as max_of_min. It is defined below, max_of_min = df.apply(lambda x: np.max(x)/np.min(x), axis=1)Finally print the max_of_minExampleLet’s check the following ... Read More

Write a Python code to find the second lowest value in each column in a given dataframe

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

874 Views

Assume, you have a dataframe and the result for second lowest value in each column as, Id       2 Salary 30000 Age    23To solve this, we will follow the steps given below −SolutionDefine a dataframeSet df.apply() function inside create lambda function and set the variable like x to access all columns and check expression asx.sort_values().unique()[1] with axis=0 to return second lowest value as, result = df.apply(lambda x: x.sort_values().unique()[1], axis=0)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, 7, 8, 9, 10], 'Salary':[20000, 30000, 50000, ... Read More

Write a program in Python to find which column has the minimum number of missing values in a given dataframe

Vani Nalliappan
Updated on 25-Feb-2021 06:48:29

321 Views

Assume, you have a dataframe and the minimum number of missing value column is, DataFrame is:    Id    Salary     Age 0 1.0    20000.0   22.0 1 2.0    NaN       23.0 2 3.0    50000.0   NaN 3 NaN    40000.0   25.0 4 5.0    80000.0   NaN 5 6.0    NaN       25.0 6 7.0    350000.0  26.0 7 8.0    55000.0   27.0 8 9.0    60000.0   NaN 9 10.0   70000.0   24.0 lowest missing value column is: IdTo solve this, we will follow the steps given ... Read More

Write a Python function to calculate the total number of business days from a range of start and end date

Vani Nalliappan
Updated on 25-Feb-2021 06:09:15

358 Views

Assume, you have a date_range of dates and the result for the total number of business days are, Dates are: DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-06',                '2020-01-07', '2020-01-08', '2020-01-09', '2020-01-10',                '2020-01-13', '2020-01-14', '2020-01-15', '2020-01-16',                '2020-01-17', '2020-01-20', '2020-01-21', '2020-01-22',                '2020-01-23', '2020-01-24', '2020-01-27', '2020-01-28',                '2020-01-29', '2020-01-30', '2020-01-31'],                dtype='datetime64[ns]', freq='B') Total number of days: 23Solution 1Define a function as business_days()set pd.bdate_range() function start ... Read More

Write a program in Python to perform flatten the records in a given dataframe by C and F order

Vani Nalliappan
Updated on 25-Feb-2021 06:06:13

113 Views

Assume, you have a dataframe and the result for flatten records in C and F order as, flat c_order:    [10 12 25 13 3 12 11 14 24 15 6 14] flat F_order:    [10 25 3 11 24 6 12 13 12 14 15 14]SolutionTo solve this, we will follow the steps given below −Define a dataframeApply df.values.ravel() function inside set an argument as order=’C’ and save it as C_order, C_order = df.values.ravel(order='C')Apply df.values.ravel() function inside set an argument as order=’F’ and save it as F_order, F_order = df.values.ravel(order='F')ExampleLet’s check the following code to get a better understanding ... Read More

Write a program in Python to print dataframe rows as orderDict with a list of tuple values

Vani Nalliappan
Updated on 25-Feb-2021 06:05:11

76 Views

Assume, you have a dataframe and the result for orderDict with list of tuples are −OrderedDict([('Index', 0), ('Name', 'Raj'), ('Age', 13), ('City', 'Chennai'), ('Mark', 80)]) OrderedDict([('Index', 1), ('Name', 'Ravi'), ('Age', 12), ('City', 'Delhi'), ('Mark', 90)]) OrderedDict([('Index', 2), ('Name', 'Ram'), ('Age', 13), ('City', 'Chennai'), ('Mark', 95)])SolutionTo solve this, we will follow the steps given below −Define a dataframeSet for loop to access all the rows using df.itertuples() function inside set name=’stud’for row in df.itertuples(name='stud')Convert all the rows to orderDict with list of tuples using rows._asdict() function and save it as dict_row. Finally print the values, dict_row = row._asdict() print(dict_row)ExampleLet’s check the ... Read More

Write a program in Python to caluculate the adjusted and non-adjusted EWM in a given dataframe

Vani Nalliappan
Updated on 25-Feb-2021 06:03:40

122 Views

Assume, you have a dataframe and the result for adjusted and non-adjusted EWM are −adjusted ewm:       Id       Age 0 1.000000 12.000000 1 1.750000 12.750000 2 2.615385 12.230769 3 2.615385 13.425000 4 4.670213 14.479339 non adjusted ewm:       Id       Age 0 1.000000 12.000000 1 1.666667 12.666667 2 2.555556 12.222222 3 2.555556 13.407407 4 4.650794 14.469136SolutionTo solve this, we will follow the steps given below −Define a dataframeCalculate adjusted ewm with delay 0.5 using df.ewm(com=0.5).mean().df.ewm(com=0.5).mean()Calculate non-adjusted ewm with delay 0.5 using df.ewm(com=0.5).mean().df.ewm(com=0.5, adjust=False).mean()Exampleimport numpy as np import pandas as pd df ... Read More

Write a Python code to fill all the missing values in a given dataframe

Vani Nalliappan
Updated on 25-Feb-2021 06:02:17

216 Views

SolutionTo solve this, we will follow the steps given below −Define a dataframeApply df.interpolate funtion inside method =’linear’, limit_direction =’forward’ and fill NaN limit = 2df.interpolate(method ='linear', limit_direction ='forward', limit = 2Exampleimport pandas as pd df = pd.DataFrame({"Id":[1, 2, 3, None, 5],                      "Age":[12, 12, 14, 13, None],                      "Mark":[80, 90, None, 95, 85],                   }) print("Dataframe is:",df) print("Interpolate missing values:") print(df.interpolate(method ='linear', limit_direction ='forward', limit = 2))OutputDataframe is:    Id     Age   Mark 0 1.0    12.0   80.0 1 2.0    12.0   90.0 2 3.0    14.0   NaN 3 NaN    13.0   95.0 4 5.0    NaN    85.0 Interpolate missing values:    Id     Age    Mark 0 1.0    12.0    80.0 1 2.0    12.0    90.0 2 3.0    14.0    92.5 3 4.0    13.0    95.0 4 5.0    13.0    85.0

Write a Python code to rename the given axis in a dataframe

Vani Nalliappan
Updated on 25-Feb-2021 06:00:35

195 Views

Assume, you have a dataframe and the result for renaming the axis is,Rename index: index    Id    Age    Mark    0    1.0    12.0   80.0    1    2.0    12.0   90.0    2    3.0    14.0   NaN    3    NaN    13.0   95.0    4    5.0    NaN    85.0SolutionTo solve this, we will follow the steps given below −Define a dataframeApply df.rename_axis() function inside axis name as ‘index’ and set axis=1df.rename_axis('index',axis=1)Exampleimport pandas as pd df = pd.DataFrame({"Id":[1, 2, 3, None, 5],                      "Age":[12, 12, 14, 13, None],                      "Mark":[80, 90, None, 95, 85],                   }) print("Dataframe is:",df) print("Rename index:") df = df.rename_axis('index',axis=1) print(df)OutputDataframe is:    Id    Age    Mark 0 1.0    12.0   80.0 1 2.0    12.0   90.0 2 3.0    14.0   NaN 3 NaN    13.0   95.0 4 5.0    NaN    85.0 Rename index: index    Id    Age    Mark    0    1.0    12.0   80.0    1    2.0    12.0   90.0    2    3.0    14.0   NaN    3    NaN    13.0   95.0    4    5.0    NaN    85.0

Advertisements