Java LocalDate get() Method with Examples

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

Java LocalDate get() Method with Examples

Explanation:

This java.time.LocalDate.get(TemporalField field) method is used to get the value of the specified field from this date as an integer. It returns the value for field.

Exceptions:

  • DateTimeException –it occurs when the value is outside the range of valid values for the field.
  • UnsupportedTemporalTypeException − it occurs if the field is not supported or the range of values exceeds an integer value.
  • ArithmeticException − it occurs when the numeric overflow occurs.

Syntax:

public int get(TemporalField field)

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

Approach:

  • Create an object of LocalDate class.
  • Then use the get method followed by specific command to get the required result.
  • Print the final result.

Program:

import java.time.LocalDate;
import java.time.temporal.ChronoField;
public class Main
{
    public static void main(String[] args)
    {
        //Create an object of LocalDate class and assign a date to it
        //here it parses the local date
        LocalDate date = LocalDate.parse("2022-05-10");
        //print the result by mentioning the specific item
      	System.out.println("Result: "+date.get(ChronoField.DAY_OF_MONTH)); 
   }
}
Output:

Result: 10

Let’s see an instance of exception with a program.

Approach:

  • Create an objects of LocalDate class which will hold the parsed dates.
  • Here we pass an invalid date for testing.
  • Then use the get method followed by specific command to get the required result.
  • Put all those code inside the try block and in catch block to check the exception.
  • Then print the final result.

Program:

import java.time.LocalDate;
import java.time.temporal.ChronoField;
public class Main
{
    public static void main(String[] args)
    {
        try
        {
            //Create an object of LocalDate class and assign a date to it
            //here it parses the local date
            LocalDate date = LocalDate.parse("2022-02-31");
            //print the result by mentioning the specific item
      	    System.out.println("Result: "+date.get(ChronoField.DAY_OF_MONTH));
        }
        catch(Exception excp)
        {
            //print the exception as result
            System.out.println(excp);
        } 
    }
}
Output:

java.time.format.DateTimeParseException: Text '2022-02-31' could not be parsed: Invalid date 'FEBRUARY 31'

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples.

Python: Open a file using “open with” statement and benefits explained with examples

Opening a file using ‘open with’ statement and benefits in Python.

In this article we will discuss about how to open a file using ‘open with’ statement, how to open multiple files in a single ‘open with’ statement and finally its benefits. So, let’s start the topic.

The need for “open with” statement :

To understand the “open with” statement we have to go through opening a file in python. For that we can make use of the open( ) function that is in-built in python

File.txt-

New File Being Read.
DONE!!
#program :

# opened a file 
fileObj = open('file.txt')
# Reading the file content into a placeholder
data = fileObj.read()
# print file content
print(data)
#close the file
fileObj.close()
Output :
New File Being Read.
DONE!!

In case the file does not exist it will throw a FileNotFoundError .

How to open a file using “open with” statement in python :

#Program :

# opened a file using open-with
with open('file.txt', "r") as fileObj:
    # Reading the file content into a placeholder
    data = fileObj.read()
    # print file content
    print(data)
# Check if file is closed
if fileObj.closed == False:
    print('File is not closed')
else:
    print('File is already closed')
New File Being Read.
DONE!!
File is closed

The with statements created an execution block that will automatically delete any object that was created in the program, in this case even if it was not closed the reader object was deleted that closed the file automatically. This saves us some memory in case we forgot to close the file.

Benefits of calling open() using “with statement” :

  • Fewer chances of bug due to coding error

With “with” statement we don’t have to close the opened file manually. It takes care of that when the compiler goes out of the block and automatically closes file. So it reduces the chances of bugs, lines of code and releases the memory for other operations.

  • Excellent handling in case of exception

If we have used “open-with” statement to open a file, and an exception occurs inside the with block, the file will be closed and the control moves to the except block.

# Python :

# Before handling the exception file will be closed 
try:
    # using "with statement" with open() function
    with open('file.txt', "r") as fileObj:
        # reading the file content
        data = fileObj.read()
        # Division by zero error
        x = 1 / 0
        print(data)
except:
    # handling the exception caused above
    print('Error occurred')
    if fileObj.closed == False:
        print('File is not closed')
    else:
        print('File is closed')
Output :
Error occurred
File is closed
  • Open multiple files in a single “with statement” :

We can use open with statement to open multiple files at the same time. Let’s try reading from one file and writing into another-

# Program :

# Read from file.txt and write in output.txt
with open('output.txt', 'w') as fileObj2, open('file.txt', 'r') as fileObj1:
    data = fileObj1.read()
    fileObj2.write(data)
    # Both the files are automatically close when the control moves out of the with block.

This will generate a “outuput.txt” file that will have the same contents as our old “file.txt”.

Output : 
Output.txt- 
New File Being Read. 
DONE !!

The files will automatically close when the control moves outside the with block.

Python: Remove characters from string by regex and 4 other ways

Removing characters from string by regex and many other ways in Python.

In this article we will get to know about deleting characters from string in python by regex() and various other functions.

Removing characters from string by regex :

sub() function of regex module in Python helps to get a new string by replacing a particular pattern in the string by a string replacement. If no pattern found, then same string will be returned.

Removing all occurrences of a character from string using regex :

Let we want to delete all occurrence of ‘a’ from a string. We can implement this by passing in sub() where it will replace all ‘s’ with empty string.

#Program :

import re
origin_string = "India is my country"

pattern = r'a'

# If found, all 'a' will be replaced by empty string
modif_string = re.sub(pattern, '', origin_string )
print(modif_string)
Output :
Indi is my country

Removing multiple characters from string using regex in python :

Let we want to delete all occurrence of ‘a’, ‘i’. So we have to replace these characters by empty string.

#Program :

import re
origin_string = "India is my country"
pattern = r'[ai]'
# If found, all 'a' & 'i' will be replaced by empty string
modif_string = re.sub(pattern, '', origin_string )
print(modif_string) 
Output :
Ind s my country

Removing characters in list from the string in python :

Let we want to delete all occurrence of ‘a’ & ‘i’ from a string and also these characters are in a list.

#Program :

import re
origin_string = "India is my country"
char_list = ['i','a']
pattern = '[' + ''.join(char_list) + ']'
# All charcaters are removed if matched by pattern
modif_string = re.sub(pattern, '', origin_string)
print(modif_string)
Output :
Ind s my country

Removing characters from string using translate() :

str class of python provide a function translate(table). The characters in the string will be replaced on the basis of mapping provided in translation table.

Removing all occurrence of a character from the string using translate() :

Let we want to delete all occurrence of ‘i’ in a string. For this we have to pass a translation table to translate() function where ‘i’ will be mapped to None.

#Program :

origin_string = "India is my country"
# If found, remove all occurence of 'i' from string
modif_string = origin_string.translate({ord('i'): None})
print(modif_string)
Output :
Inda s my country

Removing multiple characters from the string using translate() :

Let, we want to delete ‘i’, ‘y’ from the above string.

#Program :

org_string= "India is my country"
list_char = ['y', 'i']
# Remove all 's', 'a' & 'i' from the string
mod_string = org_string.translate( {ord(elem): None for elem in list_char} )
print(mod_string)
Output :
Inda s m countr

Removing characters from string using replace()  :

Python provides str class, from which the replace() returns the copy of a string by replacing all occurrence of substring by a replacement. In the above string we will try to replace all ‘i’ with ‘a’.

#Program :

origin_string = "India is my country"
# Replacing all of 's' with 'a'
modif_string = origin_string.replace('i', 'a')
print(modif_string)
Output :
Indaa as my country

Removing characters from string using join() and generator expression :

Let we have a list with some characters. For removing all characters from string that are in the list, we would iterate over each characters in the string and join them except those which are not in the list.

#Program :

origin_string= "India is my country"
list_char = ['i', 'a', 's']
# Removes all occurence of 'i', 'a','s' from the string
modif_string = ''.join((elem for elem in origin_string if elem not in list_char))
print(modif_string)
Output :
Ind  my country

Removing characters from string using join and filter() :

filter() function filter the characters from string based on logic provided in call back function. If we provide a call back function as lambda function, it checks if the characters in the list are filtered are not. After that it joins the remaining characters to from a new string i.e. it eliminates all occurrence of characters that are in the list form a string.

#Programn :

origin_string = "India is my country"
list_char = ['i', 'a', 's']
# To remove all characters in list, from the string
modif_string = ''.join(filter(lambda k: k not in list_char, origin_string))
print(modif_string)
Output :
Ind  my country

 

Pandas: Create Series from dictionary in python

Creating Series from dictionary in python

In this article we will discuss about different ways to convert a dictionary in python to a Pandas Series object.

Series class provides a constructor in Pandas i.e

Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)

Where,

  • data : It represents array-like, Iterable sequence where all items in this iterable sequence will be added as values in the Series.
  • index : It represents array-like, Iterable sequence where all values in this iterable sequence will be added as indices in the Series.
  • dtype : It represents datatype of the output series.

Create a Pandas Series from dict in python :

By passing the dictionary to the Series class Constructor i.e. Series(). we can get a new Series object where all the keys in the dictionary will become the indices of the Series object, and all the values from the key-value pairs in the dictionary will converted into the values of the Series object.

So, let’s see the example.

# Program :

import pandas as pd
# Dictionary 
dict = {
    'C': 56,
    "A": 23,
    'D': 43,
    'E': 78,
    'B': 11
}
# Converting a dictionary to a Pandas Series object.
# Where dictionary keys will be converted into index of Series &
# values of dictionar will become values in Series.
series_object = pd.Series(dict)
print('Contents of Pandas Series: ')
print(series_object)
Output :
Contents of Pandas Series: 
C  56
A  23
D  43
E  78
B  11
dtype: int64

Where the index of the series object contains the keys of the dictionary and the values of the series object contains the values of the dictionary.

Create Pandas series object from a dictionary with index in a specific order :

In the above example we observed the indices of the series object are in the same order as the keys of the dictionary. In this example we will see how to convert the dictionary into series object with some other order.

So, let’s see the example.

# Program :

import pandas as pd
# Dictionary 
dict = {
    'C': 6,
    "A": 3,
    'D': 4,
    'E': 8,
    'B': 1
}
# Creating Series from dict, but pass the index list separately
# Where dictionary keys will be converted into index of Series &
# values of dictionar will become values in Series.
# But the order of indices will be some other order
series_object = pd.Series(dict,
                       index=['E', 'D', 'C', 'B', 'A'])
print('Contents of Pandas Series: ')
print(series_object)
Output :
Contents of Pandas Series: 
E  8
D  4
C  6
B  1
A  3
dtype: int64

Create a Pandas Series object from specific key-value pairs in a dictionary :

In above examples we saw Series object is created from all the items in the dictionary as we pass the dictionary as the only argument in the series constructor. But now we will see how we will see how we can convert specific key-value pairs from dictionary to the Series object.

So, let’s see the example.

# Program :

import pandas as pd
# Dictionary 
dict = {
    'C': 6,
    "A": 3,
    'D': 4,
    'E': 8,
    'B': 1
}
# Creating Series from dict, but pass the index list separately
# Where dictionary keys will be converted into index of Series &
# values of dictionar will become values in Series.
# But here we have passed some specific key-value pairs of dictionary
series_object = pd.Series(dict,
                       index=['E', 'D', 'C'])
print('Contents of Pandas Series: ')
print(series_object)
Output :
Contents of Pandas Series: 
E 8
D 4
C 6
dtype: int64

 

Pandas : Sort a DataFrame based on column names or row index labels using Dataframe.sort_index()

Sorting a DataFrame based on column names or row index labels using Dataframe.sort_index() in Python

In this article we will discuss how we organize the content of data entered based on column names or line reference labels using Dataframe.sort_index ().

Dataframe.sort_index():

In the Python Pandas Library, the Dataframe section provides a member sort sort_index () to edit DataFrame based on label names next to the axis i.e.

DataFrame.sort_index(axis=0, level=None, ascending=True, inplace=False, kind='quicksort', na_position='last', sort_remaining=True, by=None)

Where,

  • axis :If the axis is 0, then the data name will be sorted based on the line index labels. The default is 0
  • ascending :If the axis is 1, then the data name will be sorted based on column names.
  • inplace : If the type of truth in the rise of another type arrange in order. The default is true
  • na_position :  If True, enable localization in Dataframe. Determines NaN status after filter i.e. irst puts NaNs first, finally puts NaNs at the end.

It returns the edited data object. Also, if the location dispute is untrue then it will return a duplicate copy of the provided data, instead of replacing the original Dataframe. While, if the internal dispute is true it will cause the current file name to be edited.

Let’s understand some examples,

# Program :

import pandas as pd
# List of Tuples
students = [ ('Rama', 31, 'canada') ,
             ('Symon', 23, 'Chennai' ) ,
             ('Arati', 16, 'Maharastra') ,
             ('Bhabani', 32, 'Kolkata' ) ,
             ('Modi', 33, 'Uttarpradesh' ) ,
             ('Heeron', 39, 'Hyderabad' )
              ]
# Create a DataFrame object
dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c'])
print(dfObj)
Output :
    Name   Marks  City
b  Rama     31   canada
a  Symon   23   Chennai
f   Arati      16   Maharastra
e  Bhabani  32  Kolkata
d  Modi      33  Uttarpradesh
c  Heeron  39   Hyderabad

Now let’s see how we organize this DataFrame based on labels i.e. columns or line reference labels,

Sort rows of a Dataframe based on Row index labels :

Sorting by line index labels we can call sort_index() in the data name item.

import pandas as pd
# The List of Tuples
students = [ ('Rama', 31, 'canada') ,
             ('Symon', 23, 'Chennai' ) ,
             ('Arati', 16, 'Maharastra') ,
             ('Bhabani', 32, 'Kolkata' ) ,
             ('Modi', 33, 'Uttarpradesh' ) ,
             ('Heeron', 39, 'Hyderabad' )
              ]
# To create DataFrame object 
dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c'])
# By sorting the rows of dataframe based on row index label names
modDFObj = dfObj.sort_index()
print(' Dataframes are in sorted oreder of index value given:')
print(modDFObj)
Output :
Dataframes are in sorted oreder of index value given:
    Name    Marks        City
a Symon      23         Chennai
b Rama        31         canada
c Heeron     39         Hyderabad
d Modi        33         Uttarpradesh
e Bhabani    32         Kolkata
f Arati          16         Maharastra

As we can see in the output lines it is sorted based on the reference labels now. Instead of changing the original name data backed up an edited copy of the dataframe.

Sort rows of a Dataframe in Descending Order based on Row index labels :

Sorting based on line index labels in descending order we need to pass the argument = False in sort_index() function in the data object object.

import pandas as pd
# The List of Tuples
students = [ ('Rama', 31, 'canada') ,
             ('Symon', 23, 'Chennai' ) ,
             ('Arati', 16, 'Maharastra') ,
             ('Bhabani', 32, 'Kolkata' ) ,
             ('Modi', 33, 'Uttarpradesh' ) ,
             ('Heeron', 39, 'Hyderabad' )
              ]
# To create DataFrame object 
dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c'])
# By sorting the rows of dataframe in descending order based on row index label names
conObj = dfObj.sort_index(ascending=False)
print('The Contents of Dataframe are sorted in descending Order based on Row Index Labels are of :')
print(conObj)
The Contents of Dataframe are sorted in descending Order based on Row Index Labels are of :
     Name       Marks          City
f      Arati          16          Maharastra
e     Bhabani     32          Kolkata
d     Modi        33           Uttarpradesh
c     Heeron     39           Hyderabad
b     Rama       31           canada
a     Symon     23           Chennai

As we can see in the output lines it is sorted by destructive sequence based on the current reference labels. Also, instead of changing the original data name it restored the edited copy of the data.

Sort rows of a Dataframe based on Row index labels in Place :

Filtering a local data name instead of finding the default copy transfer inplace = True in sort_index () function in the data object object to filter the data name with local reference label labels i.e.

import pandas as pd
# The List of Tuples
students = [ ('Rama', 31, 'canada') ,
             ('Symon', 23, 'Chennai' ) ,
             ('Arati', 16, 'Maharastra') ,
             ('Bhabani', 32, 'Kolkata' ) ,
             ('Modi', 33, 'Uttarpradesh' ) ,
             ('Heeron', 39, 'Hyderabad' )
              ]
# To create DataFrame object 
dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c'])
#By sorting the rows of dataframe in Place based on row index label names
dfObj.sort_index(inplace=True)
print('The Contents of Dataframe are sorted in Place based on Row Index Labels are of :')
print(dfObj)
Output :
The Contents of Dataframe are sorted in Place based on Row Index Labels are of :
     Name     Marks      City
a    Symon     23       Chennai
b     Rama     31        canada
c   Heeron     39     Hyderabad
d     Modi     33     Uttarpradesh
e  Bhabani     32       Kolkata
f    Arati       16       Maharastra

Sort Columns of a Dataframe based on Column Names :

To edit DataFrame based on column names we can say sort_index () in a DataFrame object with an axis= 1 i.e.

import pandas as pd
# The List of Tuples
students = [ ('Rama', 31, 'canada') ,
             ('Symon', 23, 'Chennai' ) ,
             ('Arati', 16, 'Maharastra') ,
             ('Bhabani', 32, 'Kolkata' ) ,
             ('Modi', 33, 'Uttarpradesh' ) ,
             ('Heeron', 39, 'Hyderabad' )
              ]
# To create DataFrame object 
dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c'])
# By sorting a dataframe based on column names
conObj = dfObj.sort_index(axis=1)
print('The Contents are of Dataframe sorted based on Column Names are in the type :')
print(conObj)

Output :
The Contents are of Dataframe sorted based on Column Names are in the type :
           City          Marks     Name
b        canada         31      Rama
a       Chennai         23     Symon
f      Maharastra     16     Arati
e       Kolkata          32     Bhabani
d  Uttarpradesh     33     Modi
c     Hyderabad     39      Heeron

As we can see, instead of changing the original data name it returns a fixed copy of the data data based on the column names.

Sort Columns of a Dataframe in Descending Order based on Column Names :

By sorting DataFrame based on column names in descending order, we can call sort_index () in the DataFrame item with axis = 1 and ascending = False i.e.

import pandas as pd
# The List of Tuples
students = [ ('Rama', 31, 'canada') ,
             ('Symon', 23, 'Chennai' ) ,
             ('Arati', 16, 'Maharastra') ,
             ('Bhabani', 32, 'Kolkata' ) ,
             ('Modi', 33, 'Uttarpradesh' ) ,
             ('Heeron', 39, 'Hyderabad' )
              ]
# To create DataFrame object 
dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c'])
# By sorting a dataframe in descending order based on column names
conObj = dfObj.sort_index(ascending=False, axis=1)
print('The Contents of Dataframe sorted in Descending Order based on Column Names are of :')
print(conObj)
Output :
The Contents of Dataframe sorted in Descending Order based on Column Names are of :
Name  Marks          City
b     Rama     31        canada
a    Symon     23       Chennai
f    Arati     16    Maharastra
e  Bhabani     32       Kolkata
d     Modi     33  Uttarpradesh
c   Heeron     39     Hyderabad

Instead of changing the original data name restore the edited copy of the data based on the column names (sorted by order)

Sort Columns of a Dataframe in Place based on Column Names :

Editing a local data name instead of obtaining an approved copy pass input = True and axis = 1 in sort_index () function in the dataframe object to filter the local data name by column names i.e.

import pandas as pd
# The List of Tuples
students = [ ('Rama', 31, 'canada') ,
             ('Symon', 23, 'Chennai' ) ,
             ('Arati', 16, 'Maharastra') ,
             ('Bhabani', 32, 'Kolkata' ) ,
             ('Modi', 33, 'Uttarpradesh' ) ,
             ('Heeron', 39, 'Hyderabad' )
              ]
# To create DataFrame object 
dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c'])
# By sorting a dataframe in place based on column names
dfObj.sort_index(inplace=True, axis=1)
print('The Contents of Dataframe sorted in Place based on Column Names are of:')
print(dfObj)

Output :
The Contents of Dataframe sorted in Place based on Column Names are of:
City  Marks     Name
b        canada     31     Rama
a       Chennai     23    Symon
f    Maharastra     16    Arati
e       Kolkata     32  Bhabani
d  Uttarpradesh     33     Modi
c     Hyderabad     39   Heeron

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

deque vs vector : What to choose ?

Deque vs Vector

In this article, we are going to see the difference between the STL sequential containers std::deque and std::vector with their appropriate usage.

VECTOR :

  • Vectors are dynamic arrays that can shrink and expand when an element is added.
  • The container handles the memory automatically.
  • The data can be inserted at the middle or at the end.
  • The elements are stored in contiguous storage.

DEQUE :

  • Deques or double-ended queues are sequence containers that shrink and expand from both the ends.
  • Data can be inserted from the start, middle and ends.
  • The data is not stored in contiguous storage locations always.

What’s the difference ?

  1. While vector is like Dynamic array. Deque is the data structure implementation of the double-ended queue.
  2. While in vector insertion and deletion at end has good performance, however insertion and deletion from middle performs badly. But deque provides the same performance like vector insertion and deletion at end for both end and middle. Also has good performance with insertion and deletion at start.
  3. While vectors store elements contiguous which makes it faster to operate at the end faster than deques. The deques are not stored contiguous but are a list of various locations where the elements are stored so overall operations at first, mid and end positions are faster than vectors as it does not have to shift elements to store an element.

Appropriate place to use :

When there are list like operations, where additions and deletion only happen at the bottom, vectors are suitable to use. In case we want to operate on the top position as well it is suitable to use deques for the purpose.

 

Java LocalDate atStartOfDay( ) Method with Example

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

Java LocalDate atStartOfDay( ) Method with Example

This java.time.LocalDate.atStartOfDay() method is used to combine the date with the time of midnight to create a LocalDate Time. It returns the local date’s time of midnight at the start of the day.

Syntax:

public LocalDateTime atStartOfDay()

Let’s see the use of atStartOfDay( ) method with 2 different format.

Method-1: without parameters

Approach:

  1. Create an object of LocalDate class which will hold the parsed date.
  2. By using that LocalDate class object call atStartOfDay() method and assign the result to an object of LocaldateTime class.
  3. Then print the final LocalDateTime as 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
    //here it parses the local date
    LocalDate date = LocalDate.parse("2022-05-10");
    System.out.println("Specific date: "+date);  
    //Create an object of LocalDateTime
    //call the atStartOfDay() method by using object of LocalDate class
    LocalDateTime startTime = date.atStartOfDay();
    System.out.println("Start Time: "+startTime);  
   }
}
Output:

Specific date: 2022-05-10
Start Time: 2022-05-10T00:00

Method-2: with parameters

Approach:

  1. Create an object of LocalDate class which will hold the parsed date.
  2. By using that LocalDate class object call atStartOfDay() method and by passing ZoneId.systemDefault() as parameter and assign the result to an object of ZoneddateTime class.
  3. Here we pass the ZoneId which is already defined by system so that it will take a particular zone and represent that zone’s start time.
  4. Then print the final ZonedDateTime as result.

Program:

import java.time.*;
public class Main
{
    public static void main(String[] args)
    {
    //Create an object of LocalDate class and assign a date to it
    //here it parses the local date
    LocalDate date = LocalDate.parse("2022-05-10");
    System.out.println("Specific date: "+date);  
    //Create an object of LocalDateTime
    //call atStartOfDay() method by passing the ZoneId.systemDefault() as parameter
    ZonedDateTime startTime = date.atStartOfDay(ZoneId.systemDefault());
    System.out.println("Start Time: "+startTime);  
   }
}
Output:

Specific date: 2022-05-10
Start Time: 2022-05-10T00:00Z[GMT]

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

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