Pandas: Sort rows or columns in Dataframe based on values using Dataframe.sort_values()

How to sort rows or columns in Dataframe based on values using Dataframe.sort_values() in Python ?

In this article we will get to know about how to sort rows in ascending & descending order based on values in columns and also sorting columns based on value in rows using member function DataFrame.sort_values().

DataFrame.sort_values() :

Pandas library contains a Dataframe class which provide a function to arrange the contents of dataframe.

Syntax : DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')

where,

  • by:  Column names or index labels where sorting is to be done.
  • axis:  If axis=0. names by argument will be taken as column names and If axis=1, names by argument will be taken as row index labels
  • ascending:  If True: Ascending Sorting , Else: Descending Sorting
  • inplace: When true in-place operation performed in dataframe
  • na_position: If True: It will sort the current dataframe , Else: Return a sorted copy of dataframe instead of modifying the dataframe.

Sorting Dataframe rows based on a single column :

To sort rows in dataframe based on ‘Team’ column, we will pass ‘Team’ as argument.

#Program :

import pandas as sc

# List of Tuples as Players
players = [ ('Curran', 325, 'CSK') ,
             ('Ishan', 481, 'MI' ) ,
             ('Vijay', 106, 'SRH') ,
             ('Pant', 224, 'Delhi' ) ,
             ('Seifert', 65, 'KKR' ) ,
             ('Hooda', 440, 'PK' )
              ]

# To Create DataFrame object
datafObjs = sc.DataFrame(players, columns=['Name', 'Runs', 'Team'], index=['i', 'ii', 'iii', 'iv', 'v', 'vi'])

# Sorting the rows of dataframe by column 'Team'
datafObjs = datafObjs.sort_values(by ='Team' )

print("Now the sorted Dataframe based on column 'Team' is : ")
print(datafObjs)
Output :
 Now the sorted Dataframe based on column 'Team' is :
        Name  Runs   Team
i     Curran   325    CSK
iv      Pant   224  Delhi
v    Seifert    65    KKR
ii     Ishan   481     MI
vi     Hooda   440     PK
iii    Vijay   106    SRH

Sorting Dataframe rows based on multiple column :

To sort all rows of dataframe based on Team & Runs column.

#Program :

import pandas as sc

# List of Tuples as Players
players = [ ('Curran', 325, 'CSK') ,
             ('Ishan', 481, 'MI' ) ,
             ('Vijay', 106, 'SRH') ,
             ('Pant', 224, 'Delhi' ) ,
             ('Seifert', 65, 'KKR' ) ,
             ('Hooda', 440, 'PK' )
              ]

# To Create DataFrame object
datafObjs = sc.DataFrame(players, columns=['Name', 'Runs', 'Team'], index=['i', 'ii', 'iii', 'iv', 'v', 'vi'])

# Sorting the rows of dataframe by column 'Team'
datafObjs = datafObjs.sort_values(by =['Runs', 'Team'])

print("Now the sorted Dataframe on the basis of columns 'Name' & 'Marks' : ")
print(datafObjs)
Output :
Now the sorted Dataframe on the basis of columns 'Name' & 'Marks' :
        Name   Runs   Team
v     Seifert    65       KKR
iii    Vijay      106     SRH
iv     Pant      224    Delhi
i      Curran   325     CSK
vi     Hooda   440     PK
ii     Ishan      481     MI

Sorting Dataframe rows based on columns in Descending Order :

Here we will sort the dataframe in descending order by passing value as false in argument ascending.

#Program :

import pandas as sc

# List of Tuples as Players
players = [ ('Curran', 325, 'CSK') ,
             ('Ishan', 481, 'MI' ) ,
             ('Vijay', 106, 'SRH') ,
             ('Pant', 224, 'Delhi' ) ,
             ('Seifert', 65, 'KKR' ) ,
             ('Hooda', 440, 'PK' )
              ]

# To Create DataFrame object
datafObjs = sc.DataFrame(players, columns=['Name', 'Runs', 'Team'], index=['i', 'ii', 'iii', 'iv', 'v', 'vi'])

# Sortimg rows of dataframe by column 'Name' in descending manner
datafObjs = datafObjs.sort_values(by ='Name' , ascending=False)

print("Now the sorted Dataframe on basis of column 'Name' in Descending manner : ")
print(datafObjs)
Output :
Now the sorted Dataframe on basis of column 'Name' in Descending manner :
        Name   Runs   Team
iii     Vijay      106    SRH
v     Seifert     65      KKR
iv    Pant       224     Delhi
ii     Ishan      481     MI
vi     Hooda   440     PK
i      Curran     325    CSK

Sorting Dataframe rows based on a column in Place :

Here we will sort the dataframe based on single column in place with argument inplace and value True.

#Program :

import pandas as sc

# List of Tuples as Players
players = [ ('Curran', 325, 'CSK') ,
             ('Ishan', 481, 'MI' ) ,
             ('Vijay', 106, 'SRH') ,
             ('Pant', 224, 'Delhi' ) ,
             ('Seifert', 65, 'KKR' ) ,
             ('Hooda', 440, 'PK' )
              ]

# To Create DataFrame object
datafObjs = sc.DataFrame(players, columns=['Name', 'Runs', 'Team'], index=['i', 'ii', 'iii', 'iv', 'v', 'vi'])

# Sorting e rows of the dataframe by column 'Name' inplace
datafObjs.sort_values(by='Team' , inplace=True)

print("Now the sorted Dataframe based on a single column 'Team' inplace : ")
print(datafObjs)
Output :
Now the sorted Dataframe based on a single column 'Team' inplace :
        Name  Runs   Team
i      Curran   325    CSK
iv     Pant     224     Delhi
v      Seifert    65    KKR
ii      Ishan    481     MI
vi     Hooda   440     PK
iii     Vijay     106     SRH

Sorting columns of a Dataframe based on a single or multiple rows :

Let’s take a example where column of a dataframe is to be sorted on the  basis of single or multiple rows.

#program :

import pandas as sc

# List of Tuples as matrix
matrix = [(11, 2, 33),
          (4, 55, 6),
          (77, 8, 99),
          ]

# To create a DataFrame object like a Matrix
datafObjs = sc.DataFrame(matrix, index=list('abc'))

print("Sorting the daataframe based on single rows or multiple rows: ")
print(datafObjs)
Output :
Sorting the daataframe based on single rows or multiple rows:
    0   1   2
a  11   2  33
b   4  55   6
c  77   8  99

Sorting columns of a Dataframe based on a single row :

Now from above dataframe let’s sort the dataframe on basis of ‘b’ row.

#program :

import pandas as sc

# List of Tuples as matrix
matrix = [(11, 2, 33),
          (4, 55, 6),
          (77, 8, 99),
          ]

# To create a DataFrame object like a Matrix
datafObjs = sc.DataFrame(matrix, index=list('abc'))
datafObjs = datafObjs.sort_values(by ='b', axis=1)

print("Now the sorted Dataframe on the basis of single row index label 'b' : ")
print(datafObjs)
Output :
Now the sorted dataframe is sorted on the basis of row index label 'b' :
    0   2   1
a  11  33   2
b   4   6  55
c  77  99   8

Sorting columns of a Dataframe in Descending Order based on a single row :

Here we will sort columns of the dataframe in descending order based on single row.

Here we will sort columns of a Dataframe on basis of multiple rows by passing value in ascending as false.

#Program :

import pandas as sc

# List of Tuples as matrix
matrix = [(11, 2, 33),
          (4, 55, 6),
          (77, 8, 99),
          ]

# To create a DataFrame object like a Matrix
datafObjs = sc.DataFrame(matrix, index=list('abc'))

# Sorting the columns of a dataframe in descending order based on a single row with index label 'b'
datafObjs = datafObjs.sort_values(by='b', axis=1, ascending=False)

print("Now the sorted Dataframe on the basis of single row index label 'b' in descending order :")
print(datafObjs)
Output :
Now the sorted Dataframe on the basis of single row index label 'b' in descending order :

    1   2   0
a   2  33  11
b  55   6   4
c   8  99  77

Sorting columns of a Dataframe based on a multiple rows :

 Here the columns of dataframe are sorted on the  basis of multiple rows with passing labels ‘b’ & ‘c’ and axis=1.

#program :

import pandas as sc

# List of Tuples as matrix
matrix = [(11, 2, 33),
          (4, 55, 6),
          (77, 8, 99),
          ]

# To create a DataFrame object like a Matrix
datafObjs = sc.DataFrame(matrix, index=list('abc'))

# Sorting columns of the dataframe based on a multiple row with index labels 'b' & 'c'
datafObjs = datafObjs.sort_values(by =['b' , 'c' ], axis=1)

print("Now the sorted Dataframe based on multiple rows index label 'b' & 'c' :")
print(datafObjs)
Output :
Now the sorted Dataframe based on multiple rows index label 'b' & 'c' :

    0   2   1
a  11  33   2
b   4   6  55
c  77  99   8

Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.

Read more Articles on Python Data Analysis Using Padas – Modify a Dataframe

Python Pandas : Drop columns in DataFrame by label Names or by Index Positions

How to drop columns in DataFrame by label Names or by Index Positions in Python ?

In this article, we are going to demonstrate how to drop columns in a dataframe by their labels or index. So, let’s start exploring the topic in detail.

In dataframe there is a function drop() which can be used to drop columns.

Syntax – DataFrame.drop

(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')

Where,

  • labels : The label or the labels we want to delete are passed here.
  • index : The index of the position to be deleted
  • columns : If it is True it deletes the columns, else the rows
  • axis : If axis is 1 it considers the columns like the columns. If 0, it works on rows.
  • inplace : If it is False then it doesn’t modify the dataframe, it returns a new one. If it is set to True it modifies the dataframe.

We will be using the following dataset as example :

     Regd     Name        Age      City           Exp
a    10         Jill             16.0     Tokyo         10
b    11         Rachel      38.0     Texas           5
c    12         Kirti          39.0      New York    7
d    13        Veena       40.0     Texas           21
e    14        Lucifer      NaN     Texas           30
f     15        Pablo        30.0     New York     7
g    16       Lionel        45.0     Colombia    11

Delete a Single column in DataFrame by Column Name :

We can delete a single column just by passing its name into the function. Let’s try deleting ‘Age’ column from the Dataframe.

Let’s see the program how to implement this.

#Program :

import numpy as np
import pandas as pd

# Example data
students = [(10,'Jill',    16,     'Tokyo',    10),
            (11,'Rachel',  38,     'Texas',     5),
            (12,'Kirti',   39,     'New York',  7),
            (13,'Veena',   40,     'Texas',    21),
            (14,'Lucifer', np.NaN, 'Texas',    30),
            (15,'Pablo',   30,     'New York',  7),
            (16,'Lionel',  45,     'Colombia', 11) ]
#Creating a dataframe object
dfObj = pd.DataFrame(students, columns=['Regd','Name','Age','City','Exp'], index=['a', 'b', 'c' , 'd' , 'e' , 'f', 'g']) 
#Modifying the dataframe and storing it into a new object
modDfObj = dfObj.drop('Age' , axis='columns')
print(modDfObj)
Output :
    Regd     Name      City         Exp
a    10        Jill           Tokyo       10
b    11      Rachel     Texas          5
c    12       Kirti       New York    7
d    13      Veena     Texas          21
e    14      Lucifer     Texas         30
f    15       Pablo     New York    7
g    16      Lionel    Colombia    11

Drop Multiple Columns by Label Names in DataFrame :

To delete multiple columns by name we just have to pass all the names as a list into the function. Let’s try deleting ‘Age’ and ‘Exp’

Let’s see the program how to implement this.

#Program :

import numpy as np
import pandas as pd

# Example data
students = [(10,'Jill',    16,     'Tokyo',    10),
            (11,'Rachel',  38,     'Texas',     5),
            (12,'Kirti',   39,     'New York',  7),
            (13,'Veena',   40,     'Texas',    21),
            (14,'Lucifer', np.NaN, 'Texas',    30),
            (15,'Pablo',   30,     'New York',  7),
            (16,'Lionel',  45,     'Colombia', 11) ]
#Creating a dataframe object
dfObj = pd.DataFrame(students, columns=['Regd','Name','Age','City','Exp'], index=['a', 'b', 'c' , 'd' , 'e' , 'f', 'g']) 
#Modifying the dataframe without the columns and storing it into a new object
modDfObj = dfObj.drop(['Age' , 'Exp'] , axis='columns')
print(modDfObj)
Output :
   Regd   Name     City
a    10     Jill          Tokyo
b    11   Rachel     Texas
c    12    Kirti        New York
d    13    Veena     Texas
e    14   Lucifer     Texas
f    15    Pablo      New York
g    16   Lionel     Colombia

Drop Columns by Index Position in DataFrame :

In case we know the index position of the columns we want to drop, we can pass them into the function. Let’s try deleting the same two columns as above but with their index position.

Let’s see the program how to implement this.

#Program :

import numpy as np
import pandas as pd

# Example data
students = [(10,'Jill',    16,     'Tokyo',    10),
            (11,'Rachel',  38,     'Texas',     5),
            (12,'Kirti',   39,     'New York',  7),
            (13,'Veena',   40,     'Texas',    21),
            (14,'Lucifer', np.NaN, 'Texas',    30),
            (15,'Pablo',   30,     'New York',  7),
            (16,'Lionel',  45,     'Colombia', 11) ]
#Creating a dataframe object
dfObj = pd.DataFrame(students, columns=['Regd','Name','Age','City','Exp'], index=['a', 'b', 'c' , 'd' , 'e' , 'f', 'g']) 
#Modifying the dataframe without the columns and storing it into a new object by passsing the index of the columns
modDfObj = dfObj.drop([dfObj.columns[2] , dfObj.columns[4]] ,  axis='columns')
print(modDfObj)
Output :
    Regd   Name      City
a    10     Jill           Tokyo
b    11    Rachel     Texas
c    12     Kirti         New York
d    13    Veena      Texas
e    14    Lucifer     Texas
f    15     Pablo       New York
g    16    Lionel      Colombia

Drop Columns in Place :

In case we don’t want a new dataframe object to hold the modified values, but want to store it in the same object, we can do it by passing inplace= True. Let’s use the previous example for this.

Let’s see the program how to implement this.

#Program :

import numpy as np
import pandas as pd

# Example data
students = [(10,'Jill',    16,     'Tokyo',    10),
            (11,'Rachel',  38,     'Texas',     5),
            (12,'Kirti',   39,     'New York',  7),
            (13,'Veena',   40,     'Texas',    21),
            (14,'Lucifer', np.NaN, 'Texas',    30),
            (15,'Pablo',   30,     'New York',  7),
            (16,'Lionel',  45,     'Colombia', 11) ]
#Creating a dataframe object
dfObj = pd.DataFrame(students, columns=['Regd','Name','Age','City','Exp'], index=['a', 'b', 'c' , 'd' , 'e' , 'f', 'g']) 
#Modifying the dataframe without the columns and storing it into the same object 
dfObj.drop([dfObj.columns[2] , dfObj.columns[4]] ,  axis='columns',inplace = True)
print(dfObj)
Output :
     Regd     Name      City
a    10          Jill         Tokyo
b    11       Rachel     Texas
c    12        Kirti       New York
d    13       Veena     Texas
e    14      Lucifer     Texas
f    15       Pablo    New York
g    16     Lionel     Colombia

Drop Column If Exists :

In case the column/row does not exist we can do a check beforehand to avoid further bugs in the program. We can do so by using the same function drop( ) , it checks for the columns and if it is not found it returns KeyError which we can handle by an if-else condition.

#program :

import numpy as np
import pandas as pd

# Example data
students = [(10,'Jill',    16,     'Tokyo',    10),
            (11,'Rachel',  38,     'Texas',     5),
            (12,'Kirti',   39,     'New York',  7),
            (13,'Veena',   40,     'Texas',    21),
            (14,'Lucifer', np.NaN, 'Texas',    30),
            (15,'Pablo',   30,     'New York',  7),
            (16,'Lionel',  45,     'Colombia', 11) ]
#Creating a dataframe object
dfObj = pd.DataFrame(students, columns=['Regd','Name','Age','City','Exp'], index=['a', 'b', 'c' , 'd' , 'e' , 'f', 'g']) 
#Checking for a non-existent column
if 'Last Name' in dfObj.columns :
    dfObj.drop('Last Name' ,  axis='columns')
    print(dfObj)
else :
    print("The column was not found")
Output :
The column was not found

Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.

Read more Articles on Python Data Analysis Using Padas – Remove Contents from a Dataframe

6 Ways to check if all values in Numpy Array are zero (in both 1D & 2D arrays) – Python

Check if all values in Numpy Array are zero (in both 1D & 2D arrays) in Python

In this article we will discuss about different ways to  check if all values in a numpy array are 0 i.e in both 1D and 2D arrays.

So let’s start exploring the topic.

Method 1: Using numpy.all() to check if a 1D Numpy array contains only 0 :

In this method we will check that each element of the array will be compared with the particular element i.e zero. And a a result it will return a bool array containing True or False.

# program :

import numpy as np

# 1D numpy array created from a list
arr = np.array([0, 0, 0, 0, 0, 0])

# Checking if all elements in array are zero
check_zero = np.all((arr == 0))
if check_zero:
    print('All the elements of array are zero')
else:
    print('All the elements of the array are not zero')
Output :
All the elements of array are zero

Method 2: Using numpy.any() to check if a 1D Numpy array contains only 0 :

We can use the numpy.any() function to check that if the array contains only zeros by looking for any non zero value. All the elements received by numpy.any() function gets typecast to bool values i.e. 0 to False and others as True. So if all the values in the array are zero then it will return False and then by using not we can confirm our array contains only zeros or not.

# program :

import numpy as np

# 1D numpy array created from a list
arr = np.array([0, 0, 0, 0, 0, 0])

# Checking if all elements in array are zero
check_zero = not np.any(arr)
if check_zero:
    print('All the elements of array are zero')
else:
    print('All the elements of the array are not zero')
Output : 
All the elements of array are zero

Method 3: Using numpy.count_nonzero() to check if a 1D Numpy array contains only 0 :

numpy.count_nonzero()  function returns a count of non-zero values in the array. So by using it we can check if the array contains any non zero value or not. If any non zero element found in the array then all the elements of the array are not zero and if no non zero value found then then all the elements in the array are zero.

# program :

import numpy as np

# 1D numpy array created from a list
arr = np.array([0, 0, 0, 0, 0, 0])

# Checking if all elements in array are zero
# Count non zero items in array
count_non_zeros = np.count_nonzero(arr)
if count_non_zeros==0:
    print('All the elements of array are zero')
else:
    print('All the elements of the array are not zero')
Output :
All the elements of array are zero

Method 4: Using for loop to check if a 1D Numpy array contains only 0 :

By iterating over all the elements of the array we also check that array contains only zeros or not.

# program :

import numpy as np

# 1D numpy array created from a list
arr = np.array([0, 0, 0, 0, 0, 0])

# Checking if all elements in array are zero
def check_zero(arr):
    # iterating of the array
    # and checking if any element is not equal to zero
    for elem in arr:
        if elem != 0:
            return False
    return True
result = check_zero(arr)

if result:
    print('All the elements of array are zero')
else:
    print('All the elements of the array are not zero')
Output :
All the elements of array are zero

Method 5: Using List Comprehension to check if a 1D Numpy array contains only 0 :

By using List Comprehension also we can iterate over each element in the numpy array and then we can create a list of values which are non zero.And if the list contains any element then we can confirm all the values of the numpy array were not zero.

# program :

import numpy as np

# 1D numpy array created from a list
arr = np.array([0, 0, 0, 0, 0, 0])

# Iterating over each element of array 
# And create a list of non zero items from array
result = len([elem for elem in arr if elem != 0])
# from this we can knoew that if our list contains no element then the array contains all zero values.

if result==0:
    print('All the elements of array are zero')
else:
    print('All the elements of the array are not zero')
Output :
All the elements of array are zero

Method 6: Using min() and max() to check if a 1D Numpy array contains only 0 :

If the minimum and maximum value in the array are same and i.e zero then we can confirm the array contains only zeros.

# program :

import numpy as np

# 1D numpy array created from a list
arr = np.array([0, 0, 0, 0, 0, 0])

if arr.min() == 0 and arr.max() == 0:
    print('All the elements of array are zero')
else:
    print('All the elements of the array are not zero')
Output :
All the elements of array are zero

Check if all elements in a 2D numpy array or matrix are zero :

Using the first technique that is by using numpy.all() function we can check the 2D array contains only zeros or not.

# program :

import numpy as np

# 2D numpy array created 
arr_2d = np.array([[0, 0, 0],
                   [0, 0, 0],
                   [0, 0, 0]])

# Checking if all 2D numpy array contains only 0
result = np.all((arr_2d == 0))

if result:
    print('All elemnets of the 2D array are zero')
else:
    print('All elemnets of the 2D array are not zero')
Output : 
All the elements of the 2D array are zero

Java LocalDate equals( ) Method with Example

In this article we are going to see the use of Java LocalDate class equals( ) method with suitable examples.

Java LocalDate equals( ) Method with Example

This java.time.LocalDate.equals(Object obj) method is used to check a date with another date whether both the dates are equal or not. It returns Boolean value, returns true if equal, false if not equal.

Syntax:

public boolean equals(Object obj)

Where,

  • obj refers to the date which will be passed as parameter to check it is equal with another date or not.

Let’s see the program to understand it more clearly.

Approach:

  • Create two objects of LocalDate class which will hold the parsed dates, here we have taken date1 and date2.
  • Then by using equal() method check both dates are equal or not like date2.equals(date1).
  • Print the final result.

Program:

CASE-1: When both the dates are not equal

import java.time.LocalDate;
public class Main
{
    public static void main(String[] args) 
    {
        //Create an object of LocalDate class and assign a date to it.
     	LocalDate date1 = LocalDate.parse("2022-04-25");
      	System.out.println("date-1: "+date1);
      	//Create an object of LocalDate class and assign a date to it..
      	LocalDate date2 = LocalDate.parse("2022-05-08");
      	System.out.println("date-2: "+date2);
      	//check both the values equal or not and print the final result.
      	System.out.println("Result: "+date2.equals(date1)); 
   	}
}
Output:

date-1: 2022-04-25
date-2: 2022-05-08
Result: false

CASE-2: When both the dates are equal

import java.time.LocalDate;
public class Main
{
    public static void main(String[] args) 
    {
        //Create an object of LocalDate class and assign a date to it.
         LocalDate date1 = LocalDate.parse("2022-05-18");
          System.out.println("date-1: "+date1);
          //Create an object of LocalDate class and assign a date to it..
          LocalDate date2 = LocalDate.parse("2022-05-18");
          System.out.println("date-2: "+date2);
          //check both the values equal or not and print the final result.
          System.out.println("Result: "+date2.equals(date1)); 
       }
}
Output:

date-1: 2022-05-18
date-2: 2022-05-18
Result: true

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Java LocalDate atTime( ) Method with Example

In this article we are going to see the use of Java LocalDate class atTime() method with suitable examples.

Java LocalDate atTime( ) Method with Example

This java.time.LocalDate.atTime(LocalTime time) method is used to combine a date with time to create LocalDateTime. It returns the local date-time formed by the specified date and time.

Syntax:

public LocalDateTime atTime(LocalTime time)

Where,

  • time refers to the actual time to be combined with the date.

Let’s see the use of atTime() method.

Method-1: Java LocalDateTime class atTime(int hour, int minute)

The LocalDate.atTime(int hour, int minute) method creates a LocalDateTime by combining the date with a specified time. Here hour and minute are passed as the parameter to the atTime() method.

It returns the formed local date-time but if any value is out of range then it gives DateTimeException.

Syntax:

public LocalDateTime atTime(int hour, int minute)

Where,

  • hour refers to the hour of the day, its value ranges from 0 to 23.
  • minute refers to the minute of the hour, its value ranges from 0 to 59.

Approach:

  • Create an object of LocalDate class which will hold the parsed date.
  • Then pass a time as parameter in (int hour, int minute) format to atTime() method which will add the specified time with the provided date and the final local date-time result will be hold by an object of LocalDateTime class.
  • Print the final result.

Program:

import java.time.LocalDate;
import java.time.LocalDateTime;
public class Main
{
   public static void main(String[] args) 
    {
        //Create an object of LocalDate class and assign a date to it
        LocalDate date = LocalDate.parse("2017-02-03");
        System.out.println("Specified date: "+date);  
        //Create an object of LocalDateTime class 
        //By using the object of LocalDate class and atTime() method create the local date-time
        LocalDateTime dateWithTime = date.atTime(1,20);
        //Print the result
        System.out.println("Final Date and Time: "+dateWithTime);  
    }
}
Output:

Specified date: 2017-02-03
Final Date and Time: 2017-02-03T01:20

Method-2: Java LocalDateTime class atTime(int hour, int minute, int second)

The LocalDate.atTime(int hour, int minute, int second) method creates a LocalDateTime by combining the date with a specified time. Here hour, minute and second are passed as the parameter to the atTime() method.

It returns the formed local date-time but if any value is out of range then it gives DateTimeException.

Syntax:

public LocalDateTime atTime(int hour, int minute, int second)

Where,

  • hour refers to the hour of the day, its value ranges from 0 to 23.
  • minute refers to the minute of the hour, its value ranges from 0 to 59.
  • second refers to the seconds of the minute, its value ranges from 0 to 59.

Approach:

  • Create an object of LocalDate class which will hold the parsed date.
  • Then pass a time as parameter in (int hour, int minute, int second) format to atTime() method which will add the specified time with the provided date and the final local date-time result will be hold by an object of LocalDateTime class.
  • Print the final result.

Program:

import java.time.LocalDate;
import java.time.LocalDateTime;
public class Main
{
    public static void main(String[] args) 
    {
        //Create an object of LocalDate class and assign a date to it
        LocalDate date = LocalDate.parse("2017-02-03");
        System.out.println("Specified date: "+date);  
        //Create an object of LocalDateTime class 
        //By using the object of LocalDate class and atTime() method create the local date-time
        LocalDateTime dateWithTime = date.atTime(1,20,25);
        //Print the result
        System.out.println("Final Date and Time: "+dateWithTime);  
    }
}
Output:

Specified date: 2017-02-03
Final Date and Time: 2017-02-03T01:20:25

Method-3: Java LocalDateTime class atTime(int hour, int minute, int second, int nanoOfSecond)

The LocalDate.atTime(int hour, int minute, int second, int nanoOfSecond) method creates a LocalDateTime by combining the date with a specified time. Here hour, minute, second and Nano second are passed as the parameter to the atTime() method.

It returns the formed local date-time but if any value is out of range then it gives DateTimeException.

Syntax:

public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond)

Where,

  • hour refers to the hour of the day, its value ranges from 0 to 23.
  • minute refers to the minute of the hour, its value ranges from 0 to 59.
  • second refers to the seconds of the minute, its value ranges from 0 to 59.
  • nanoOfSecond refers to the Nano seconds of the second, its value ranges from 0 to 999999999.

Approach:

  • Create an object of LocalDate class which will hold the parsed date.
  • Then pass a time as parameter in (int hour, int minute, int second, int nanoOfSecond) format to atTime() method which will add the specified time with the provided date and the final local date-time result will be hold by an object of LocalDateTime class.
  • Print the final result.

Program:

import java.time.LocalDate;
import java.time.LocalDateTime;
public class Main
{
    public static void main(String[] args) 
    {
        //Create an object of LocalDate class and assign a date to it
        LocalDate date = LocalDate.parse("2017-02-03");
        System.out.println("Specified date: "+date);  
        //Create an object of LocalDateTime class 
        //By using the object of LocalDate class and atTime() method create the local date-time
        LocalDateTime dateWithTime = date.atTime(1,20,25,345);
        //Print the result
        System.out.println("Final Date and Time: "+dateWithTime);  
    }
}
Output:

Specified date: 2017-02-03
Final Date and Time: 2017-02-03T01:20:25.000000345

Method-4: Java LocalDateTime class atTime(LocalTime time)

The LocalDate.atTime(LocalTime time) method creates a LocalDateTime by combining the date with a specified time. Here specific time is passed as the parameter to the atTime() method.

It returns the formed local date-time but if any value is out of range then it gives DateTimeException.

Syntax:

public LocalDateTime atTime(LocalTime time)

Where,

  • time refers to the time in LocalTime format.

Approach:

  • Create an object of LocalDate class which will hold the parsed date.
  • Create an object of LocalTime class which will hold the parsed time.
  • Then pass a time as parameter in (LocalTime time) format to atTime() method which will add the specified time with the provided date and the final local date-time result will be hold by an object of LocalDateTime class.
  • Print the final result.

Program:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class Main
{
    public static void main(String[] args) 
    {
        //Create an object of LocalDate class and assign a date to it
        LocalDate date = LocalDate.parse("2017-02-03");
        System.out.println("Specified date: "+date);
        //Create an object of LocalTime class and pass a parse value of time 
        LocalTime time = LocalTime.parse("09:20:45");
        //Create an object of LocalDateTime class 
        //By using the object of LocalDate class and atTime() method create the local date-time
        LocalDateTime dateWithTime = date.atTime(time );
        //Print the result
        System.out.println("Final Date and Time: "+dateWithTime);  
    }
}
Output:

Specified date: 2017-02-03
Final Date and Time: 2017-02-03T09:20:45

Method-5: OffsetDateTime atTime(OffsetTime time)

The OffsetDateTime atTime(OffsetTime time) method creates a OffsetDateTime by combining the date with a offset time. Here specific time is passed as the parameter to the atTime() method.

It returns the formed local date-time but if any value is out of range then it gives DateTimeException.

Syntax:

public OffsetDateTime atTime(OffsetTime time)

Where,

  • time refers to the time in OffsetTime format.

Approach:

  • Create an object of LocalDate class which will hold the parsed date.
  • Create an object of OffsetTime class which will hold the offset time.
  • Then pass a time as parameter in (OffsetTime time) format to atTime() method which will add the specified time with the provided date and the final local date-time result will be hold by an object of LocalDateTime class.
  • Print the final result.

Program:

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
public class Main
{
    public static void main(String[] args) 
    {
        //Create an object of LocalDate class and assign a date to it
        LocalDate date = LocalDate.parse("2017-02-03");
        System.out.println("Specified date: "+date);
        //Create an object of OffsetTime class 
        OffsetTime time = OffsetTime.now();
        //Create an object of LocalDateTime class 
        //By using the object of OffsetDateTime class and atTime() method create the local date-time
        OffsetDateTime dateWithTime = date.atTime(time );
        //Print the result
        System.out.println("Final Date and Time: "+dateWithTime);  
    }
} 
Output:

Specified date: 2017-02-03
Final Date and Time: 2017-02-03T14:17:35.166418Z

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Java Program to Remove Duplicate Elements from a Singly Linked List

In this article we are going to see how we remove duplicate elements from a singly linked list by using Java programming language.

Java Program to Remove Duplicate Elements from a Singly Linked List

Approach:

  • Create a linked list.
  • Add some elements(with duplicates) to it.
  • Print the list.
  • Call the user defined method removeDup( ) that iterates the whole list repeatedly and removes all the duplicate nodes from the list.
  • Display the new list.

Program:

import java.util.*;
// Main class
public class Main
{
    // Class Node that defines the two linked list variables
    class Node
    {
        int data;
        Node nextNode;
        // constructor to create a node
        public Node(int data) 
        {    
            this.data = data;    
            this.nextNode = null;    
        }    
    }

    // Setting the head and end of the node as NULL  
    public Node head = null;    
    public Node tail = null;  

    // Method to remove duplicate elements from a linked list
    public void removeDup() 
    {  
        Node curr = head, index = null, temp = null;  
        if(head == null) 
        {  
            System.out.println("THe linked list is empty");
            return;  
        }  
        else 
        {  
            while(curr != null)
            {  
                //Node temp points to previous node to index.  
                temp = curr;  
                //Index points to the next node
                index = curr.nextNode;  
                while(index != null) 
                {  
                    //If curr node's data is equal to index node's data  
                    if(curr.data == index.data) 
                    {  
                        //If we find a duplicate node it skips the duplicate node by pointing to nextNode node  
                        temp.nextNode = index.nextNode;  
                    }  
                    else 
                    {  
                        //Temp points to the previous node in the index
                        temp = index;  
                    }  
                    index = index.nextNode;  
                }  
                curr = curr.nextNode;  
            }  
        }  
    }

    // method to add a node to the end of the linked list
    public void add(int data)
    {
        Node newNode = new Node(data);
        // Checks if there was any previous node
        if(head==null)
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            tail.nextNode = newNode;
            tail = newNode;
        }
    }
    
    // Method to display all the nodes of the linked list
    public void show()
    {
        Node curr = head;
        // If the head is pointing to no node then the linked list is empty
        if(head==null)
            System.out.println("Linked List is empty");
        else
        {
            System.out.println("The nodes are:");
            while(curr!=null)
            {
                System.out.print(curr.data+",");
                curr = curr.nextNode;
            }
            System.out.println();
        }
    }

    public static void main(String[] args) 
    {
        // create an object of the main class
        Main ll = new Main();
        // add elements to the linked list
        ll.add(10);
        ll.add(20);
        ll.add(30);
        ll.add(40);
        ll.add(50);
        ll.add(20);
        ll.add(40);
        // display the nodes
        ll.show();
        System.out.println("Removing the duplicate nodes...");
        ll.removeDup();
        // display the nodes
        ll.show();
    }
}
Output:

The nodes are:
10,20,30,40,50,20,40,
Removing the duplicate nodes...
The nodes are:
10,20,30,40,50,

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Pandas : Find duplicate rows in a Dataframe based on all or selected columns using DataFrame.duplicated() in Python

How to find duplicate rows in a Dataframe based on all or selected columns using DataFrame.duplicate() in Python ?

In this article we will discuss about how we can find duplicate rows in a Dataframe based on all or selected columns using DataFrame.duplicated(). So first let’s know about this duplicated() function then we will see how it actually works.

DataFrame.duplicated()

Python’s Pandas library contains DataFrame class which provides a function i.e. duplicated() that helps in finding duplicate rows based on specific or all columns.

Synatx : DataFrame.duplicated (subset='None', keep='first')

where,

  • subset : It represents single or multiple column labels which will be used for duplication check. If it is not provided then all columns will be checked for finding duplicate rows.
  • keep : It represents the occurrence which needs to be marked as duplicate. Its value can be (first : here all duplicate rows except their first occurrence are returned and it is the default value also, last : here all duplicate rows except their last occurrence are returned and false : here all duplicate rows except occurrence are returned)

Find Duplicate rows based on all columns :

To find all the duplicate rows based on all columns, we should not pass any argument in subset while calling DataFrame.duplicate(). If any duplicate rows found, True will be returned at place of the duplicated rows expect the first occurrence as default value of keep argument is first.

import pandas as sc

# List of Tuples

players = [('MI', 'Surya', 487),

('RR', 'Buttler', 438),

('CSK', 'Jadeja', 456),

('CSK', 'Jadeja', 456),

('KKR', 'Gill', 337),

('SRH', 'Roy', 241),

('DC', 'Rahane', 221),

('CSK', 'Dhoni', 446),

('PK', 'Malan', 298)

]

# To create a DataFrame object

dfObjs = sc.DataFrame(players, columns=['Team', 'Player', 'Runs'])

# To select duplicate rows based on all columns except the first occurrence

dupliRows = dfObjs[dfObjs.duplicated()]

print("Duplicate rows based on all column excluding first occurrence is:")

print(dupliRows)
Output :
Duplicate rows based on all column excluding first occurrence is:
Team  Player  Runs
3  CSK  Jadeja   456


In the above example all duplicate values returned except the first occurrence, because the by default value of keep is first.

Note : If we make keep argument as last then while finding the duplicate rows last occurrence will be ignored.

Find Duplicate Rows based on selected columns :

If we want to find duplicate compare rows on selected column, then we should pass the columns names as argument in duplicate(), which will return the duplicate rows based on passed or selected columns. Similarly in this case also first occurrence is ignored.

#Program :

import pandas as sc

# List of Tuples

players = [('MI', 'Surya', 487),

('RR', 'Buttler', 438),

('DC', 'Pant', 337),

('CSK', 'Dhoni', 456),

('KKR', 'Gill', 337),

('SRH', 'Roy', 241),

('DC', 'Rahane', 337),

('DC', 'Iyer', 337),

('PK', 'Malan', 298)

]

# To create a DataFrame object

dfObjs = sc.DataFrame(players, columns=['Team', 'Player', 'Runs'])

# Select all duplicate rows based on one column

# To select the duplicated rows based on column that is passed as argument

dupliRows = dfObjs[dfObjs.duplicated(['Team','Runs'])]

print("Duplicate Rows based on a selected column are:", dupliRows, sep='\n')
Output :
Duplicate Rows based on a selected column are:
     Team   Player      Runs
6    DC      Rahane    337
7    DC      Iyer          337

By default value of keep is first, so only matched first row is ignored. Here, we have found rows based on selected columns. In this example we have selected  columns (Team and Runs) based on which 3 rows matches i.e

'DC', 'Pant', 337
'DC', 'Rahane', 337
'DC', 'Iyer', 337

Out of which last two rows are displayed and first row is ignored as the keep value is first(default value).

So it returned last two rows as output i.e

'DC', 'Rahane', 337
'DC', 'Iyer', 337

Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.

Read more Articles on Python Data Analysis Using Padas – Find Elements in a Dataframe

R: Set working directory

How to set work directory in R studio ?

In this article we are going to discuss about how we can set work directory R and also we will verify if that directory has been set perfectly or not. So, let’s start exploring the topic.

R is an interpreted programming language which is created by Ross Ihaka and Robert Gentleman. This programming language and open source environment is used for statistical computing, graphical presentation, data analytics and scientific research.

R studio is an  Integrated Development Environment (IDE) which provides free and open source tools for Rn language.

When working with R language sometimes we need to work with external files and for that we have to set that file directory as working directory otherwise the file won’t be accessible.

So, let’s see how we can set a directory as an working directory in R studio.

Setting up a Working Directory in R studio :

Steps to set working directory in R studio.

  1. Go to session menu.
  2. Select set working directory.
  3. Then select Choose Directory.
  4. From there browse the directory.

Set up Working Directory in R using setwd() function :

By using setwd() function we can set the working directory.

Syntax : setwd("D:/R_workingfile")

For example, we have a directory /Users/BtechGeeks and we want to set it as  working directory then we can do it like this

# command to set working directory.
setwd("Users/BtechGeeks")

If the path does not exist :

If you want to set a directory as working directory but that directory does not exist then it will give Error.

For example, a directory /Users/Btech which does not exists but we want to make it working directory.

Then

# Directory does not exist
setwd("/Users/Btech")
# Raising error as directory does not exit
Error in setwd("/Users/Btech") : cannot change working directory

Verifying the current working directory is set in R :

We can use getwd() function to check the current working directory.

If it returns the correct directory then the working directory has been set perfectly.

# It will give the current working directory
getwd()
>> "/Users/BtechGeeks"

Java Program to Search for an Element in a Singly Linked List

In this article we are going to see how we can search for an element in a singly linked list by using Java programming language.

Java Program to Search for an Element in a Singly Linked List

Approach:

  • Create a linked list.
  • Add elements to it.
  • Display the list.
  • Ask the user to enter an element to search.
  • Pass the element into our user defined method search( ) method. The method iterates the whole list, compares the value.
  • Then prints the result.

Program:

import java.util.*;
// Main class
public class Main
{
    // Class Node that defines the two linked list variables
    class Node
    {
        int data;
        Node nextNode;
        // constructor to create a node
        public Node(int data) 
        {    
            this.data = data;    
            this.nextNode = null;    
        }    
    }

    // Setting the head and end of the node as NULL  
    public Node head = null;    
    public Node tail = null;  

    // method to search for a value in a linked list
    public void search(int data) 
    {  
        Node curr = head;  
        // iterator
        int i = 1;  
        // Flag to check the condition
        boolean flag = false;  
        if(head == null) 
        {  
            System.out.println("Linked list is empty");  
        }
        else 
        {  
            // Iterates the whole list
            while(curr != null)
            {  
                // compares the value with the data at each node
                if(curr.data == data) 
                {
                    // If a match is found breaks out of the loop  
                    flag = true;  
                    break;  
                }  
                i++;  
                curr = curr.nextNode;  
            }  
        }  
        if(flag)  
            System.out.println("Element is at location : " + i);  
        else  
            System.out.println("Element could not be found");  
    }  

    // method to add a node to the end of the linked list
    public void add(int data)
    {
        Node newNode = new Node(data);
        // Checks if there was any previous node
        if(head==null)
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            tail.nextNode = newNode;
            tail = newNode;
        }
    }
    
    // Method to display all the nodes of the linked list
    public void show()
    {
        Node curr = head;
        // If the head is pointing to no node then the linked list is empty
        if(head==null)
            System.out.println("Linked List is empty");
        else
        {
            System.out.println("The nodes are:");
            while(curr!=null)
            {
                System.out.print(curr.data+",");
                curr = curr.nextNode;
            }
            System.out.println();
        }
    }

    public static void main(String[] args) 
    {
        // create an object of the main class
        Main ll = new Main();
        // add elements to the linked list
        ll.add(10);
        ll.add(20);
        ll.add(30);
        ll.add(40);
        ll.add(50);
        // display the nodes
        ll.show();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a value to search");
        int val = sc.nextInt();
        ll.search(val);
    }
}
Output:

The nodes are:
10,20,30,40,50,
Enter a value to search
50
Element is at location : 5

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Java Program to Find the Max and Min Values in a Linked List

In this article we are going to see how we can find the max and min values in a linked list by using Java programming language.

Java Program to Find the Max and Min Values in a Linked List

Approach:

  • Create a linked list.
  • Add elements to the list.
  • Display the elements.
  • Call the user defined method minVal() and maxVal() method to find the min and max value from the list. The method stores the head element in variable and compares it with the data from the rest of the list and prints the min/max value.

Program:

import java.util.*;
// Main class
public class Main
{
    // Class Node that defines the two linked list variables
    class Node
    {
        int data;
        Node nextNode;
        // constructor to create a node
        public Node(int data) 
        {    
            this.data = data;    
            this.nextNode = null;    
        }    
    }

    // Setting the head and end of the node as NULL  
    public Node head = null;    
    public Node tail = null;  

    // method to find the minimum value in the linked list  
    public void minVal() 
    {  
        Node curr = head;  
        int min;  
        if(head == null)
        {  
            System.out.println("Linked list is empty");  
        }  
        else 
        {  
            //Store the first node data
            min = head.data;  
            while(curr != null)
            {  
                //compare min with each node and if any value is smaller than min replace min with it.
                if(min > curr.data) 
                {  
                    min = curr.data;  
                }  
                curr= curr.nextNode;  
            }  
            System.out.println("Minimum value in the list is "+ min);  
        }  
    }  

    // method to find the maximum value in the linked list  
    public void maxVal() 
    {  
        Node curr = head;  
        int max;  
        if(head == null) 
        {  
            System.out.println("Linked list is empty");  
        }  
        else 
        {  
            //Storing the first node data in max  
            max = head.data;  
            while(curr != null)
            {  
                //compare max with each node and if any value is bigger than max replace min with it.  
                if(max < curr.data) 
                {  
                    max = curr.data;  
                }  
                curr = curr.nextNode;  
            }  
            System.out.println("Maximum value in the list is "+ max);  
        }  
    }  

    // method to add a node to the end of the linked list
    public void add(int data)
    {
        Node newNode = new Node(data);
        // Checks if there was any previous node
        if(head==null)
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            tail.nextNode = newNode;
            tail = newNode;
        }
    }
    
    // Method to display all the nodes of the linked list
    public void show()
    {
        Node curr = head;
        // If the head is pointing to no node then the linked list is empty
        if(head==null)
            System.out.println("Linked List is empty");
        else
        {
            System.out.println("The nodes are:");
            while(curr!=null)
            {
                System.out.print(curr.data+",");
                curr = curr.nextNode;
            }
            System.out.println();
        }
    }

    public static void main(String[] args) 
    {
        // create an object of the main class
        Main ll = new Main();
        // add elements to the linked list
        ll.add(10);
        ll.add(20);
        ll.add(30);
        ll.add(40);
        ll.add(50);
        // display the nodes
        ll.show();
        ll.minVal();
        ll.maxVal();
    }
}
Output:

The nodes are:
10,20,30,40,50,
Minimum value in the list is 10
Maximum value in the list is 50

Grab the opportunity to learn all effective java programming language concepts from basic toadvance levels by practicing these Java Program Examples with Output.