Programming Articles

Page 2023 of 2544

Write a program in Python to remove first duplicate rows in a given dataframe

Vani Nalliappan
Vani Nalliappan
Updated on 25-Feb-2021 333 Views

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 More

Write a program in Python to compute grouped data covariance and calculate grouped data covariance between two columns in a given dataframe

Vani Nalliappan
Vani Nalliappan
Updated on 25-Feb-2021 429 Views

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 More

Write 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

Vani Nalliappan
Vani Nalliappan
Updated on 24-Feb-2021 113 Views

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 More

Write a program to truncate a dataframe time series data based on index value

Vani Nalliappan
Vani Nalliappan
Updated on 24-Feb-2021 375 Views

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 More

Write a program in Python to compute autocorrelation between series and number of lags

Vani Nalliappan
Vani Nalliappan
Updated on 24-Feb-2021 303 Views

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 More

Write a program in Python to export a given dataframe into Pickle file format and read the content from the Pickle file

Vani Nalliappan
Vani Nalliappan
Updated on 24-Feb-2021 235 Views

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 More

Write a program in Python to resample a given time series data and find the maximum month-end frequency

Vani Nalliappan
Vani Nalliappan
Updated on 24-Feb-2021 230 Views

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

Write a Python program to read an Excel data from file and read all rows of first and last columns

Vani Nalliappan
Vani Nalliappan
Updated on 24-Feb-2021 1K+ Views

Assume, you have an Excel file stored with the name of pandas.xlsx in your location.SolutionTo solve this, we will follow the steps given below −Define pd.read_excel method to read data from pandas.xlsx file and save it as dfdf = pd.read_excel('pandas.xlsx')Apply df.iloc[:, 0] to print all rows of first columndf.iloc[:, 0]Apply df.iloc[:, -1] to print all rows of last columndf.iloc[:, -1]ExampleLet’s see the below implementation to get a better understanding −import pandas as pd df = pd.read_csv('products.csv') print("all rows of first column is") print(df.iloc[:, 0]) print("all rows of last column is") print(df.iloc[:, -1])Outputall rows of first column is 0     ...

Read More

Write a program in Python to read CSV data from a file and print the total sum of last two rows

Vani Nalliappan
Vani Nalliappan
Updated on 24-Feb-2021 2K+ Views

Assume you have the following data in your csv file and save it as pandas.csv.pandas.csvId, Data 1, 11 2, 22 3, 33 4, 44 5, 55 6, 66 7, 77 8, 88 9, 99 10, 100The result for sum of last two records as, Sum of last two rows: Id    9 Data 99Solution 1Access stored data from csv file and save it as data using the below method, data = pd.read_csv('pandas.csv')Convert the data into dataframe and store inside df, df = pd.DataFrame(data)Apply the below method to take last two records and calculate the sum, df.tail(2)).sum()ExampleLet’s see the below implementation ...

Read More

Write a Python program to export dataframe into an Excel file with multiple sheets

Vani Nalliappan
Vani Nalliappan
Updated on 24-Feb-2021 947 Views

Assume, you have a dataframe and the result for export dataframe to multiple sheets as, To solve this, we will follow the steps given below −Solutionimport xlsxwriter module to use excel conversionDefine a dataframe and assign to dfApply pd.ExcelWriter function inside name excel name you want to create and set engine as xlsxwriterexcel_writer = pd.ExcelWriter('pandas_df.xlsx', engine='xlsxwriter')Convert the dataframe to multiple excel sheets using the below method, df.to_excel(excel_writer, sheet_name='first_sheet') df.to_excel(excel_writer, sheet_name='second_sheet') df.to_excel(excel_writer, sheet_name='third_sheet')Finally save the excel_writerexcel_writer.save()ExampleLet’s understand the below code to get a better understanding −import pandas as pd import xlsxwriter df = pd.DataFrame({'Fruits': ["Apple", "Orange", "Mango", "Kiwi"],       ...

Read More
Showing 20221–20230 of 25,433 articles
Advertisements