Found 33676 Articles for Programming

Write a program to separate date and time from the datetime column in Python Pandas

Vani Nalliappan
Updated on 25-Feb-2021 07:05:47

10K+ Views

Assume, you have datetime column in dataframe and the result for separating date and time as,    datetime    date          time 0 2020-01-01 07:00:00 2020-01-06 07:00:00 1 2020-01-02 07:00:00 2020-01-06 07:00:00 2 2020-01-03 07:00:00 2020-01-06 07:00:00 3 2020-01-04 07:00:00 2020-01-06 07:00:00 4 2020-01-05 07:00:00 2020-01-06 07:00:00 5 2020-01-06 07:00:00 2020-01-06 07:00:00To solve this, we will follow the below approaches −Solution 1Define a dataframe ‘datetime’ column using pd.date_range(). It is defined below, pd.DataFrame({'datetime':pd.date_range('2020-01-01 07:00', periods=6)})Set for loop d variable to access df[‘datetime’] column one by one.Convert date and time from for loop and save it as df[‘date’] ... Read More

Write a program in Python to print numeric index array with sorted distinct values in a given series

Vani Nalliappan
Updated on 25-Feb-2021 07:01:19

124 Views

Assume, you have a series and the numberic index with sorted distinct values are −Sorted distict values - numeric array index [2 3 0 3 2 1 4] ['apple' 'kiwi' 'mango' 'orange' 'pomegranate']To solve this, we will follow the steps given below −SolutionApply pd.factorize() function inside list of non-unique elements and save it as index, index_value.index, unique_value = pd.factorize(['mango', 'orange', 'apple', 'orange', 'mango', 'kiwi', 'pomegranate'])Print the index and elements. Result is diplayed without sorting of distinct values and its indexApply pd.factorize() inside list elements and set sort=True then save it as sorted_index, unique_valuesorted_index, unique_value = pd.factorize(['mango', 'orange', 'apple', 'orange', 'mango', ... Read More

Write a program in Python to perform average of rolling window size 3 calculation in a given dataframe

Vani Nalliappan
Updated on 25-Feb-2021 07:00:07

200 Views

Assume, you have a dataframe and the result for rolling window size 3 calculation is, Average of rolling window is:    Id Age  Mark 0 NaN NaN  NaN 1 1.5 12.0 85.0 2 2.5 13.0 80.0 3 3.5 13.5 82.5 4 4.5 31.5 90.0 5 5.5 60.0 87.5To solve this, we will follow the below approach −SolutionDefine a dataframeApply df.rolling(window=2).mean() to calculate average of rolling window size 3 isdf.rolling(window=2).mean()ExampleLet’s check the following code to get a better understanding −import pandas as pd df = pd.DataFrame({"Id":[1, 2, 3, 4, 5, 6],                     ... Read More

Write a program in Python to slice substrings from each element in a given series

Vani Nalliappan
Updated on 25-Feb-2021 06:58:31

127 Views

Assume, you have a series and the result for slicing substrings from each element in series as, 0    Ap 1    Oa 2    Mn 3    KwTo solve this, we will follow the below approaches −Solution 1Define a seriesApply str.slice function inside start=0, stop-4 and step=2 to slice the substring from the series.data.str.slice(start=0, stop=4, step=2)ExampleLet’s check the following code to get a better understanding −import pandas as pd data = pd.Series(['Apple', 'Orange', 'Mango', 'Kiwis']) print(data.str.slice(start=0, stop=4, step=2))Output0    Ap 1    Oa 2    Mn 3    KwSolution 2Define a seriesApply string index slice to start from 0 ... Read More

How can augmentation be used to reduce overfitting using Tensorflow and Python?

AmitDiwan
Updated on 22-Feb-2021 06:49:34

330 Views

Augmentation can be used to reduce overfitting by adding additional training data. This is done by creating a sequential model that uses a ‘RandomFlip’ layer.Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.A neural network that contains at least one layer is known as a convolutional layer. We can use the Convolutional Neural Network to build learning ... Read More

How can Tensorflow be used to visualize training results using Python?

AmitDiwan
Updated on 22-Feb-2021 06:42:04

887 Views

The training results can be visualized with Tensorflow using Python with the help of the ‘matplotlib’ library. The ‘plot’ method is used to plot the data on the console.Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.A neural network that contains at least one layer is known as a convolutional layer. We can use the Convolutional Neural Network ... Read More

Write a Python function to split the string based on delimiter and convert to series

Vani Nalliappan
Updated on 25-Feb-2021 06:55:36

755 Views

The result for splitting the string with ’' delimiter and convert to series as, 0    apple 1    orange 2    mango 3    kiwiTo solve this, we will follow the below approach −Solution 1define a function split_str() which accepts two arguments string and delimiterCreate s.split() function inside delimiter value and store it as split_datasplit_data = s.split(d)Apply split_data inside pd.Series() to generate series data.pd.Series(split_data)Finally, call the function to return the result.ExampleLet’s check the following code to get a better understanding −import pandas as pd def split_str(s, d):    split_data = s.split(d)    print(pd.Series(split_data)) split_str('apple\torange\tmango\tkiwi', '\t')Output0    apple 1   ... Read More

Write a program in Python to print the first and last three days from a given time series data

Vani Nalliappan
Updated on 25-Feb-2021 06:52:25

202 Views

Assume, you have time series and the result for the first and last three days from the given series as, first three days: 2020-01-01    Chennai 2020-01-03    Delhi Freq: 2D, dtype: object last three days: 2020-01-07    Pune 2020-01-09    Kolkata Freq: 2D, dtype: objectTo solve this, we will follow the steps given below −SolutionDefine a series and store it as data.Apply pd.date_range() function inside start date as ‘2020-01-01’ and periods = 5, freq =’2D’ and save it as time_seriestime_series = pd.date_range('2020-01-01', periods = 5, freq ='2D')Set date.index = time_seriesPrint the first three days using data.first(’3D’) and save it ... Read More

Write a program in Python to generate a random array of 30 elements from 1 to 100 and calculate maximum by minimum of each row in a dataframe

Vani Nalliappan
Updated on 25-Feb-2021 06:51:25

113 Views

Result for generating dataframe maximum by a minimum of each row is0    43.000000 1    1.911111 2    2.405405 3    20.000000 4    7.727273 5    6.333333To solve this, we will follow the steps given below −Solution 1Define a dataframe with size of 30 random elements from 1 to 100 and reshape the array by (6, 5) to change 2-D arraydf = pd.DataFrame(np.random.randint(1, 100, 30).reshape(6, 5))Create df.apply function inside lambda method to calculate np.max(x)/np.min(x) with axis as 1 and save as max_of_min. It is defined below, max_of_min = df.apply(lambda x: np.max(x)/np.min(x), axis=1)Finally print the max_of_minExampleLet’s check the following ... Read More

Write a Python code to find the second lowest value in each column in a given dataframe

Vani Nalliappan
Updated on 25-Feb-2021 06:49:42

1K+ Views

Assume, you have a dataframe and the result for second lowest value in each column as, Id       2 Salary 30000 Age    23To solve this, we will follow the steps given below −SolutionDefine a dataframeSet df.apply() function inside create lambda function and set the variable like x to access all columns and check expression asx.sort_values().unique()[1] with axis=0 to return second lowest value as, result = df.apply(lambda x: x.sort_values().unique()[1], axis=0)ExampleLet’s check the following code to get a better understanding −import pandas as pd df = pd.DataFrame({'Id':[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'Salary':[20000, 30000, 50000, ... Read More

Advertisements