Found 33676 Articles for Programming

Write a Python program to reshape a given dataframe in different ways

Vani Nalliappan
Updated on 25-Feb-2021 05:24:37

461 Views

We can reshape a dataframe using melt(), stack(), unstack() and pivot() function.Solution 1Define a dataframe.Apply melt() function to convert wide dataframe column as rows. It is defined below, df.melt()ExampleLet’s see the below code to get a better understanding −import pandas as pd df = pd.DataFrame({'Id':[1, 2, 3], 'Age':[13, 14, 13], 'Mark':[80, 90, 85]}) print("Dataframe is:", df) print(df.melt())OutputDataframe is:  Id Age Mark 0 1 13   80 1 2 14   90 2 3 13   85 variable value 0    Id    1 1    Id    2 2    Id    3 3   Age    13 4   ... Read More

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

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

329 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
Updated on 24-Feb-2021 10:33:27

264 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
Updated on 24-Feb-2021 10:31:34

205 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 Python code to read JSON data from a file and convert it to dataframe, CSV files

Vani Nalliappan
Updated on 24-Feb-2021 10:28:26

1K+ Views

Assume you have the following sample json data stored in a file as pandas_sample.json{    "employee": {       "name": "emp1",       "salary": 50000,       "age": 31    } }The result for after converting to csv as, , employee age, 31 name, emp1 salary, 50000SolutionTo solve this, we will follow the steps given below −Create pandas_sample.json file and store the JSON data.Read json data from the file and store it as data.data = pd.read_json('pandas_sample.json')Convert the data into dataframedf = pd.DataFrame(data)Apple df.to_csv function to convert the data as csv file format, df.to_csv('pandas_json.csv')ExampleLet’s see the below implementation ... Read More

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

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

200 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 export a dataframe to an html file

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

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

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

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

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

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

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

890 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

Advertisements