Vani Nalliappan has Published 130 Articles

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

Vani Nalliappan

Vani Nalliappan

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

455 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], ... 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 10:42:20

84 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 ... 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 10:35:10

327 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 ... 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 10:33:27

263 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 ... 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 10:31:34

204 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 ... Read More

Write a Python code to read JSON data from a file and convert it to dataframe, CSV files

Vani Nalliappan

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 ... 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 10:27:30

197 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: ... Read More

Write a Python program to export a dataframe to an html file

Vani Nalliappan

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 ... 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 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 ... 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 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 ... Read More

Previous 1 ... 6 7 8 9 10 ... 13 Next
Advertisements