Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1069 of 3363
805 Views
To generate dates in a range, use the date _range() method. At first, import the required pandas library with an alias −import pandas as pdNow, let’s say you need to generate dates in arrange, therefore for this, mention the date from where you want to begin. Here, we have mentioned 1st June 2021 and period of 60 days −dates = pd.date_range('6/1/2021', periods=60) ExampleFollowing is the complete code − import pandas as pd # generate dates in a range # period is 60 i.e. 60 days from 1st June 2021 dates = pd.date_range('6/1/2021', periods=60) print"Displaying dates in a range...", ... Read More
414 Views
To convert string data to actual dates i.e. datetime type, use the to_datetime() method. At first, let us create a DataFrame with 3 categories, one of the them is a date string −dataFrame = pd.DataFrame({ 'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Stationery'], 'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Chairs'], 'Date_of_Purchase': ['10/07/2021', '20/04/2021', '25/06/2021', '15/02/2021'], }) Convert date strings to actual dates using to_datetime() −dataFrame['Date_of_Purchase'] = pd.to_datetime(dataFrame['Date_of_Purchase'])ExampleFollowing is the complete code −import pandas as pd # create a dataframe dataFrame = pd.DataFrame({ 'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Stationery'], 'Product Name': ['Keyboard', 'Charger', 'SmartTV', ... Read More
185 Views
To compute last of group values, use the groupby.last() method. At first, import the required library with an alias −import pandas as pd;Create a DataFrame with 3 columns −dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'BMW', 'Tesla', 'Lexus', 'Tesla'], "Place": ['Delhi', 'Bangalore', 'Pune', 'Punjab', 'Chandigarh', 'Mumbai'], "Units": [100, 150, 50, 80, 110, 90] } ) Now, group DataFrame by a column −groupDF = dataFrame.groupby("Car")Compute last of group values and resetting index −res = groupDF.last() res = res.reset_index()ExampleFollowing is the complete code. The last occurrence of repeated values are displayed i.e. last of group values ... Read More
902 Views
To filter on the basis of sum of columns, we use the loc() method. Here, in our example, we sum the marks of each student to get the student column with marks above 400 i.e. 80%.At first, create a DataFrame with student records. We have marks records of 3 students i.e 3 columns −dataFrame = pd.DataFrame({ 'Jacob_Marks': [95, 90, 75, 85, 88], 'Ted_Marks': [60, 50, 65, 85, 70], 'Jamie_Marks': [77, 76, 65, 45, 50]}) Filtering on the basis of columns. Fetching student with total marks above 400 −dataFrame = dataFrame.loc[:, dataFrame.sum(axis=0) > 400]ExampleFollowing is the complete ... Read More
219 Views
To select first periods of time series based on a date offset, use the first() method. At first, set the date index with periods and freq parameters. Freq is for frequency −i = pd.date_range('2021-07-15', periods=5, freq='3D')Now, create a DataFrame with above index −dataFrame = pd.DataFrame({'k': [1, 2, 3, 4, 5]}, index=i) Fetch rows from first 4 days i.e. 4D −dataFrame.first('4D')ExampleFollowing is the complete code − import pandas as pd # date index set with 5 periods and frequency of 3 days i = pd.date_range('2021-07-15', periods=5, freq='3D') # creating DataFrame with above index dataFrame = pd.DataFrame({'k': [1, 2, 3, ... Read More
5K+ Views
To merge Pandas DataFrame, use the merge() function. In that, you can set the parameter indicator to True or False. If you want to check which dataframe has a specific record, then use −indicator= TrueAs shown above, using above parameter as True, adds a column to the output DataFrame called “_merge”.At first, let us import the pandas library with an alias −import pandas as pd Let us create DataFrame1 −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, ... Read More
835 Views
To calculate the standard deviation, use the std() method of the Pandas. At first, import the required Pandas library −import pandas as pdNow, create a DataFrame with two columns −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) Finding the standard deviation of “Units” column value using std() −print"Standard Deviation of Units column from DataFrame1 = ", dataFrame1['Units'].std()In the same way, we have calculated the standard deviation from the 2nd DataFrame.ExampleFollowing is the complete code −# # Python - Calculate the ... Read More
212 Views
To select final periods of time series based on a date offset, use the last() method. At first, set the date index with periods and freq. Freq is for frequency −i = pd.date_range('2021-07-15', periods=5, freq='3D')Now, create a DataFrame with above index −dataFrame = pd.DataFrame({'k': [1, 2, 3, 4, 5]}, index=i) Fetch rows from last 4 days i.e. 4D −dataFrame.last('4D')ExampleFollowing is the complete code −import pandas as pd # date index set with 5 periods and frequency of 3 days i = pd.date_range('2021-07-15', periods=5, freq='3D') # creating DataFrame with above index dataFrame = pd.DataFrame({'k': [1, 2, 3, 4, 5]}, ... Read More
1K+ Views
To remove leading or trailing whitespace, use the strip() method. At first, create a DataFrame with 3 columns “Product Category”, “Product Name” and “Quantity” −dataFrame = pd.DataFrame({ 'Product Category': [' Computer', ' Mobile Phone', 'Electronics ', 'Appliances', ' Furniture', 'Stationery'], 'Product Name': ['Keyboard', 'Charger', ' SmartTV', 'Refrigerators', ' Chairs', 'Diaries'], 'Quantity': [10, 50, 10, 20, 25, 50]})Removing whitespace from more than one column −dataFrame['Product Category'].str.strip() dataFrame['Product Name'].str.strip()ExampleFollowing is the complete code −import pandas as pd # create a dataframe with 3 columns dataFrame = pd.DataFrame({ 'Product Category': [' Computer', ' Mobile Phone', 'Electronics ', 'Appliances', ... Read More
996 Views
When it is required to convert a matrix into a string, a simple list comprehension along with the ‘join’ method is used.ExampleBelow is a demonstration of the samemy_list = [[1, 22, "python"], [22, "is", 1], ["great", 1, 91]] print("The list is :") print(my_list) my_list_1, my_list_2 = ", ", " " my_result = my_list_2.join([my_list_1.join([str(elem) for elem in sub]) for sub in my_list]) print("The result is :") print(my_result)OutputThe list is : [[1, 22, 'python'], [22, 'is', 1], ['great', 1, 91]] The result is : 1, 22, python 22, is, 1 great, 1, 91ExplanationA list of list is defined ... Read More