Remove Columns from DataFrame in Python

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

201 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

Find Most Repeated Element in a Series Using Python

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

354 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

Select Random Odd Index Rows in a DataFrame using Python

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

704 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

Merge Two DataFrames Based on Matching Data in Python

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

245 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

Generate Random Rows of Vowels in Python

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

92 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

Modify Diagonal of a DataFrame by 1 in Python

Vani Nalliappan
Updated on 24-Feb-2021 09:36:03

395 Views

Assume, you have a dataframe0 1 20 10 20 30 1 40 50 60 2 70 80 90The result for replaced 1 by diagonal of a dataframe is −0 1 2 0 1 20 30 1 40 1 60 2 70 80 1SolutionTo solve this, we will follow the steps given below −Define a dataframeCreate nested for loop to access all rows and columns, for i in range(len(df)):    for j in range(len(df)):Check if the condition to match the diagonals, if it is matched then replace the position by 1. It is defined below, if i == j:    df.iloc[i ... Read More

Find Lowest Value in DataFrame and Store it in Python

Vani Nalliappan
Updated on 24-Feb-2021 09:31:05

393 Views

Assume you have a dataframe, one two three 0 12 13 5 1 10 6 4 2 16 18 20 3 11 15 58The result for storing the minimum value in new row and column is −Add new column to store min value  one   two  three min_value 0 12    13   5       5 1 10    6    4       4 2 16    18  20      16 3 11    15  58      11 Add new row to store min value    one   two   three min_value 0   ... Read More

Read Sample Data from SQL Database in Python

Vani Nalliappan
Updated on 24-Feb-2021 09:24:08

588 Views

Assume you have a sqlite3 database with student records and the result for reading all the data is,   Id Name 0 1 stud1 1 2 stud2 2 3 stud3 3 4 stud4 4 5 stud5SolutionTo solve this, we will follow the steps given below −Define a new connection. It is shown below, con = sqlite3.connect("db.sqlite3")Read sql data from the database using below function, pd.read_sql_query()Select all student data from table using read_sql_query with connection, pd.read_sql_query("SELECT * FROM student", con)ExampleLet us see the complete implementation to get a better understanding −import pandas as pd import sqlite3 con = sqlite3.connect("db.sqlite3") df = ... Read More

Python Program for Boolean Logical Operations

Vani Nalliappan
Updated on 24-Feb-2021 09:22:02

157 Views

Assume you have a series and the result for Boolean operations, And operation is: 0    True 1    True 2    False dtype: bool Or operation is: 0    True 1    True 2    True dtype: bool Xor operation is: 0    False 1    False 2    True dtype: boolSolutionTo solve this, we will follow the below approach.Define a SeriesCreate a series with boolean and nan valuesPerform boolean True against bitwise & operation to each element in the series defined below, series_and = pd.Series([True, np.nan, False], dtype="bool") & TruePerform boolean True against bitwise | operation ... Read More

Transpose Index and Columns in a Given DataFrame Using Python

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

308 Views

Input −Assume you have a DataFrame, and the result for transpose of index and columns are, Transposed DataFrame is   0 1 0 1 4 1 2 5 2 3 6Solution 1Define a DataFrameSet nested list comprehension to iterate each element in the two-dimensional list data and store it in result.result = [[data[i][j] for i in range(len(data))] for j in range(len(data[0]))Convert the result to DataFrame, df2 = pd.DataFrame(result)ExampleLet us see the complete implementation to get a better understanding −import pandas as pd data = [[1, 2, 3], [4, 5, 6]] df = pd.DataFrame(data) print("Original DataFrame is", df) result = [[data[i][j] ... Read More

Advertisements