In this blog post, we will show you how to compare things in Python. We’ll start with a simple example of comparing two series objects. If these are not identically labeled then they can’t be compared using the cmp() function.
We’re going to create some seed data for our demonstration: three numbers that represent different temperatures at various locations over time a day, a month and an entire year respectively: 30 degrees Fahrenheit or 15 degrees Celsius, 60 degrees Fahrenheit or 15 degrees Celsius and 20 degrees Fahrenheit (-17Celsius). These values will be saved in a variable called temps.
The first thing we’ll do is create three series objects, labeled ‘day’, ‘month’ and ‘year’. The cmp() function can only compare two identically-labeled sequence objects so if the third object doesn’t have an “x” appended to it’s name then they can’t be compared with one another:
temp_d = pd . Series ([ 30 , 60 ], index = range ( 0 ))
temp_m = pd . Series ([ 15 , 20 ] + [ ‘.x’ ])
temp_y = pd . Series ([ -15 , 17 ] + [ ‘.x’] )
print(pd.cmp([temps], temp_d, temp_m)) # 0
print(pd.cmp([temps], temp_y)) # nan
This example is just a simple demonstration of how you can use the cmp() function to compare two series objects in Python. There are many other functions that allow for more complicated comparisons so be sure to explore your options and see what works best for your needs!
In this example, we’ll create three different Series objects: one labeled with an X (temps) which contains values between -15 and 17; another without the initial X label but containing data from 0-60 degrees Celsius (-15 through 30); and finally one with no labels at all which will contain numbers starting from zero up until 60 degrees Celsius (0-30). We can then call pd.cmp(), passing it any number of arguments, and the result will be a complete ordering for all objects passed to it which can then be used in various ways.
The cmp() function will return an integer value, with -ve values indicating that the first object is less than or equal to the second, and positive integers (like one) if they are considered the same by this comparison. We’ll find out more about how we can use these numbers after running some comparisons on our series objects!
We can see that the first series object is less than or equal to all other objects, with a value of -140. The second has a cmp() value of 180 as it’s greater than both the third and fourth in this comparison. Finally we have two objects which are considered ‘equal’ by our function at 150!
This means that if you want your code to be able to handle comparisons between different types of data then you’ll need to use labels for each type so they can be compared correctly (you may also find some information here about how best to do such an operation). If not, Python will only compare identically-labeled series objects with one another; but don’t worry: there are more functions in the pandas library to help you out.
can only compare identically-labeled series objects – but don’t worry! There are more functions in the pandas library to help you out.
and (if present) denote a break in content, usually for images or tables that we will not be discussing here today.
For the most part, the functions for plotting data are not much different from those for reading and manipulating data – they can be used with either type of object. You should consider using them when working with many types of Python objects (lists, dicts) as well as pandas Series or DataFrame objects.
can only compare identically-labeled series objects but don’t worry! There are more functions in the pandas library to help you out. For the most part, the functions for plotting data are not much different from those for reading and manipulating data – they can be used with either type of object. You should consider using them when working with many types of Python objects (lists, dicts) as well as pandas Series or DataFrame objects. The first function we’ll explore is “str()”. This will display a string representation of your data that may include some useful information about how it was created or what’s inside it: “`python x = pd.Series ([0, 0, 1000]) x.str()