Found 33676 Articles for Programming

Write a Python program to separate a series of alphabets and digits and convert them to a dataframe

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

162 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

Write a program in Python to filter armstrong numbers in a given series

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

324 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

Write a Python program to shuffle all the elements in a given series

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

278 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

Write a program in Python to covert the datatype of a particular column in a dataframe

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

122 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

Write a Python code to swap last two rows in a given dataframe

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

643 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

Write a program in Python to remove one or more than one columns in a given DataFrame

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

191 Views

Assume, you have a dataframe,  one  two three 0 1    2    3 1 4    5    6And the result for removing single column is,  two three 0 2    3 1 5    6The result for removing after more than one column is,  three 0 3 1 6To solve this, we will follow the steps given below −Solution 1Define a dataframeDelete a particular column using below method, del df['one']ExampleLet’s see the below code to get a better understanding −import pandas as pd data = [[1, 2, 3], [4, 5, 6]] df = pd.DataFrame(data, columns=('one', 'two', 'three')) print("Before ... Read More

Write a program in Python to find the most repeated element in a series

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

340 Views

Assume, you have the following series, Series is: 0    1 1    22 2    3 3    4 4    22 5    5 6    22And the result for the most repeated element is, Repeated element is: 22SolutionTo solve this, we will follow the below approach, Define a seriesSet initial count is 0 and max_count value as series first element value data[0]count = 0 max_count = data[0]Create for loop to access series data and set frequency_count as l.count(i)for i in data:    frequency_count = l.count(i)Set if condition to compare with max_count value, if the condition is true ... Read More

Write a program in Python to select any random odd index rows in a given DataFrame

Vani Nalliappan
Updated on 24-Feb-2021 09:43:40

694 Views

Assume, you have a dataframe, DataFrame is:  id mark age 0 1 70   12 1 2 60   13 2 3 40   12 3 4 50   13 4 5 80   12 5 6 90   13 6 7 60   12And, the result for selecting any random odd index row is, Random odd index row is:  id    4 mark   50 age    13SolutionTo solve this, we will follow the steps given below −Define a dataframeCreate an empty list to append odd index valuesCreate a for loop to access all the index. It is defined ... Read More

Write a program in Python to merge two dataframes based on matching data in a column

Vani Nalliappan
Updated on 24-Feb-2021 09:41:12

236 Views

Assume, you have two dataframe, first dataframe is  id country 0 1 India 1 2 UK 2 3 US 3 4 China second dataframe is  id City 0 1 Chennai 1 11 Cambridge 2 22 Chicago 3 4 ChengduAnd the result for merging based on same column is, Merging data based on same column - id  id    country    City 0 1    India    Chennai 1 4    China    ChengduSolutionTo solve this, we will follow the steps given below −Define a two dataframesMerge two dataframes based on the same column id is defined below, pd.merge(first_df, ... Read More

Write a Python program to generate random ten rows, two columns of vowels. If both the rows are matched with same vowels, then print the index and count of matched columns

Vani Nalliappan
Updated on 24-Feb-2021 09:38:34

84 Views

Assume, you have a dataframe,  col1 col2 0 o    e 1 e    e 2 i    u 3 e    o 4 i    i 5 u    o 6 e    a 7 u    o 8 a    u 9 e    aThe result for matched index and count is, index is  col1 col2 1 e    e 4 i    i count is 2SolutionTo solve this, we will follow the steps given below −Define a dataframeCompare first and second matching index values using the below method, df.iloc[np.where(df.col1==df.col2)])Find the total count of matched columns using the ... Read More

Advertisements