Resample Time Series Data and Find Maximum Month-End Frequency in Python

Vani Nalliappan
Updated on 24-Feb-2021 10:27:30

207 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

Export DataFrame to HTML File in Python

Vani Nalliappan
Updated on 24-Feb-2021 10:20:19

2K+ Views

Assume, we have already saved pandas.csv file and export the file to Html formatSolutionTo solve this, we will follow the steps given below −Read the csv file using the read_csv method as follows −df = pd.read_csv('pandas.csv')Create new file pandas.html in write mode using file object, f = open('pandas.html', 'w')Declare result variable to convert dataframe to html file format, result = df.to_html()Using the file object, write all the data from the result. Finally close the file object, f.write(result) f.close()ExampleLet’s see the below implementation to get a better understanding −import pandas as pd df = pd.read_csv('pandas.csv') print(df) f = open('pandas.html', 'w') result ... Read More

Read Excel Data and Extract First and Last Columns in Python

Vani Nalliappan
Updated on 24-Feb-2021 10:17:30

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

Read CSV Data and Print Total Sum of Last Two Rows in Python

Vani Nalliappan
Updated on 24-Feb-2021 10:14:12

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

Export DataFrame to Excel with Multiple Sheets in Python

Vani Nalliappan
Updated on 24-Feb-2021 10:10:19

900 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

Separate Alphabets and Digits in Python and Convert to DataFrame

Vani Nalliappan
Updated on 24-Feb-2021 10:06:48

174 Views

Assume you have a series and the result for separating alphabets and digits and store it in dataframe as, series is: 0    abx123 1    bcd25 2    cxy30 dtype: object Dataframe is    0   1 0 abx 123 1 bcd 25 2 cxy 30To solve this, we will follow the below approach, SolutionDefine a series.Apple series extract method inside use regular expression pattern to separate alphabets and digits then store it in a dataframe −series.str.extract(r'(\w+[a-z])(\d+)')ExampleLet’s see the below implementation to get a better understanding −import pandas as pd series = pd.Series(['abx123', 'bcd25', 'cxy30']) print("series is:", series) df ... Read More

Filter Armstrong Numbers in a Given Series using Python

Vani Nalliappan
Updated on 24-Feb-2021 10:03:55

336 Views

Assume you have a series and the result for filtering armstrong numbers, original series is 0    153 1    323 2    371 3    420 4    500 dtype: int64 Armstrong numbers are:- 0    153 2    371 dtype: int64To solve this, we will follow the steps given below −Define a series.Create an empty list and set for loop to access all the series data.Set armstrong intial value is 0 and create temp variable to store series elements one by one. It is defined below, l = [] for val in data:    armstrong = 0   ... Read More

Shuffle Elements in a Given Series using Python

Vani Nalliappan
Updated on 24-Feb-2021 10:01:05

287 Views

Assume, you have a dataframe and the result for shuffling all the data in a series, The original series is 0    1 1    2 2    3 3    4 4    5 dtype: int64 The shuffled series is : 0    2 1    1 2    3 3    5 4    4 dtype: int64Solution 1Define a series.Apply random shuffle method takes series data as an argument and shuffles it.data = pd.Series([1, 2, 3, 4, 5]) print(data) rand.shuffle(data)ExampleLet’s see the below code to get a better understanding −import pandas as pd import random as rand data ... Read More

Convert DataType of Column in DataFrame using Python

Vani Nalliappan
Updated on 24-Feb-2021 09:58:53

132 Views

Assume, you have a dataframe, the result for converting float to int as, Before conversion Name      object Age       int64 Maths     int64 Science   int64 English   int64 Result    float64 dtype: object After conversion Name    object Age     int64 Maths   int64 Science int64 English int64 Result int64 dtype: objectTo solve this, we will follow the steps given below −SolutionDefine a dataframeConvert float datatype column ‘Result’ into ‘int’ as follows −df.Result.astype(int)ExampleLet’s see the below implementation to get a better understanding −import pandas as pd data = {'Name': ['David', 'Adam', ... Read More

Swap Last Two Rows in a Given DataFrame using Python

Vani Nalliappan
Updated on 24-Feb-2021 09:56:07

654 Views

Assume you have dataframe and the result for swapping last two rows, Before swapping   Name    Age Maths Science English 0 David   13   98      75    79 1 Adam    12   59      96    45 2 Bob     12   66      55    70 3 Alex    13   95      49    60 4 Serina  12   70      78    80 After swapping    Name  Age Maths Science English 0 David   13   98    75    79 1 Adam    12   59 ... Read More

Advertisements