15 Useful Things Things You Can Do in One Line of Python Code

What will we cover?

In this tutorial you will learn why One Liners is something beginners focus on and senior developers don’t waste their time on. But You will learn some useful things you can do in one line of Python code.

Why are One-Liners not (always) good?

Why are one-liners are bad?

If I could say it one word: Readability.

Most beginners, I was the same, are focus on solving the problem, and later, often solving it in some impressive way.

Why do senior developers not do that? Well, they spend hours debugging code – code should be easy to understand and maintain.

Yes, senior developers know that code that can written in one line, is often difficult to get useful stack traces from, when they fail. They know it is better to focus on breaking the code up in multiple lines. This makes the stack trace easier to get the error. Also, it enhances the readability of the code. Which makes it easier to understand and therefore to maintain.

Now let’s dive into 15 useful things you still can do in one line of Python code without compromising readability.

#1 Swap Two Variables

This one might not be fully appreciated if you have not been coding in another language.

Take a moment and think about the following problem.

You have two drinks.

Your job is to switch the content of the glass. That is, the blue drink should be in the glass of the red drink, and vice verse.

Obviously, to do this, you need a third glass or something similar.

This is the same problem when you need to swap (switch) the the “content” of two variables.

As you need to do this often as a programmer, Python has made this easy for you.

a = 10
b = 20

print(a, b)

# Swap the two variables
a, b = b, a

print(a, b)

This will swap the content. First print will output 10 20 and second 20 10.

#2 Reverse a List

First of all, Python lists are amazing. Again, if you worked with other programming languages, you will fall in love with how easy it is to work with Python lists.

Anyhow, sometimes you need to get the content from a list in reversed order.

This can be done easily as follows.

l = [1, 2, 3, 4, 5]

print(l[::-1])

This will output 5, 4, 3, 2, 1.

Check out the last bonus trick how this can be useful to know for a job interview.

#3 Calculate the mode of a list

First of all, what is the mode of a list?

Good question my friend. It is the most common element in a list. This is often useful to know.

Let’s see how this can be done.

l = [1,3,2,5,2,2,5,4]

mode = max(set(l), key=l.count)

print(mode)

This will output 2, as it is the most common element.

How to understand the code?

I am happy you asked.

The set(l) gives a set of the list, which is all the unique element in the list. Here it give {1, 3, 2, 5, 4}.

Then max(set(l), key=l.count) gives the maximum value of each value in set with the count of it. Hence, you get the value with the highest count.

#4 Strip lines for start and end spaces and remove new lines

When you read lines from a text file, it can have leading and ending spaces, as well as lines with no content.

This is an example of lines read from a text file.

lines = ['                       THE ADVENTURE OF THE NOBLE BACHELOR\n',
 '\n',
 '     The Lord St. Simon marriage, and its curious termination, have long\n']

To remove (or strip) for leading and ending spaces, you can do the following.

lines = [line.strip() for line in lines]

print(lines)

Which will result in.

['THE ADVENTURE OF THE NOBLE BACHELOR',
 '',
 'The Lord St. Simon marriage, and its curious termination, have long']

Notice it also remove new lines.

If you want to remove empty lines. This can be done as follows.

lines = [line for line in lines if len(line) > 0]

print(lines)

This will result in.

['THE ADVENTURE OF THE NOBLE BACHELOR',
 'The Lord St. Simon marriage, and its curious termination, have long']

#5 Multiple variable assignment

Sometimes code can become really long, if you have a lot of variable you need to assign to specific values.

This can be done in one line.

a, b, c = 4.4, 'Awesome', 7

print(a)
print(b)
print(c)

This will output.

4.4
Awesome
7

Notice the different types of the variables.

#6 Convert a string into a number

I actually love this one. Why? Because in Python it just a built-in function to convert a string to number.

my_str = '27'
my_int = int(my_str)
print(my_int, type(my_int))

my_str = '3.14'
my_float = float(my_str)
print(my_float, type(my_float))

It will print the values and the type of the variables, int and float, respectively.

#7 Type casting a list of items

This is a great use of List Comprehension.

Say, you have a list of strings with integers. It can happen you read a text file, and each line has integers. Then you need to convert them to integers to use the values.

This can be done as follows.

l = ['12', '23', '34']

items = [int(i) for i in l]

Wow. Did you see that? We just used what we learned in last step and combine it with List Comprehensions.

#8 Find the square root of a number

This is quite handy to know how to take the square root of a number without using math libraries.

print(16**.5)

Well, the 16**.5 syntax (the double **) puts the value (here 16) to the power of the exponent (here .5). This lifts 16 to the power of a half (.5). This is the same as taking the square root.

Hence, it will print 4.

#9 How to get the cube root of a number

This one is almost the same. But remember, you get a bonus one in the end, so you will get 15 one-liners that useful, if you feel cheated by this one.

The cube root means, given a number x, find a number y such that y*y*y equals x.

How do you do that?

I actually expect that many do not know that. I didn’t before I studied high-level math in college.

Here we go.

print(27**(1/3))

Ah, you see. You lift to the power of one third. It will print 3, as 3*3*3 is 27.

#10 Get the absolute value of a number

Again a great built-in function to know.

I often need the absolute values of a number. This can be achieved by using abs().

a = -27

print(abs(a))

This will print 27.

#11 Round a number to n digits

If you’ve been working with floats, you know the pain of endless long digits.

Another great built-in function in Python will help you here.

pi = 3.1415

pi_two_digits = round(pi, 2)

print(pi_two_digits)

This will print 3.14.

#12 Create a list of numbers in specific range

I actually used this one all the time before. And I loved to use it with Python loops.

Let’s see what it is.

my_list = list(range(7, 23))

print(my_list)

This will generate the following list.

[7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]

If you need to iterate a specific number of times.

for i in range(100):
    print(i)

This will iterate from 0 to 99 and print the values.

#13 Calculate the average of a list

Now does the use of this need to be explained?

You have a list of numbers, you need the average of the numbers in the list?

One line of Python will do it for you.

l = [1, 2, 3, 4, 5]

average = sum(l)/len(l)

print(average)

It will print 3.0, as it is the average of the list.

#14 If-else assignment

First I wasn’t really fan of this one. But when you keep it simple, it is useful. The key is not to have complex checks to keep the readability good.

x = 19

v = 42 if x < 10 else 27

print(v)

This will output 27, as x is greater than 10. Try it with x = 5 and see it will print 42.

#15 Flatten a list of lists

First of all, what does does flatten a list of list mean?

Given a list of lists.

l = [[1,2], [4, 6], [8, 10]]

How do you get a list of all the numbers, like this one?

[1, 2, 4, 6, 8, 10]

You do that as follows.

flat = [i for j in l for i in j]

(Bonus) Check if word is a palindrome

What does it mean that a word is a palindrome?

That it is spelled the same from back and front.

The typical example is racecar .

See, it is identical spelled backwards and forwards.

How can you check that a word is a palindrome? Obviously in one line of code? Yes, you should be able to do that now after this list.

Extra bonus: This is a typical job interview question. Be sure to nail this one and impress them.

Here we go.

s = 'racecar'

print(s == s[::-1])

This will print True as it is a palindrome.

s = 'palindrome'

print(s == s[::-1])

This will print False, as it is not a palindrome (except the string is palindrome).

How does it work?

Well, s[::-1] is the reverse of s. If the reverse of s equals s, then it must be a palindrome.

Want to learn more?

f you are hooked on learning Python I will suggest you follow my beginners course on Python.

It is well structured and has focus on you as a learner.

I suggest you break it down as explained in #1.

  • Day 1: See lesson with new concepts – take notes.
  • Day 2: Recap lesson (either from notes or video) – then see introduction to project. Try to solve the project and stop when you get stuck.
  • Day 3: Possibly recap lesson again, then continue with project. If you are really stuck – see solution.

Then continue that pattern for each lesson.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

9 Reasons Why Most Fail Learning Python and How to Avoid Them

What will we cover?

In this tutorial we will look at some of the common reasons why most fail a learning Python, but more interestingly, how to avoid to fall in that category.

The good news is, that everyone can learn Python. I have been working as a developer for 15 years and seen people getting from no knowledge or programming to mastering it at a decent level. This includes people of all ages. Simply said, there is no reason why you shouldn’t be able to learn it too.

That said, it will be a challenging ride and nobody is just born naturally as a programmer. But it depends on many factors how big a challenge it will be. With proper forces and understanding of what you will face, you can increase your chances to 100% of mastering Python and get paid a great salary as well as getting many opportunities you do not get in other industries.

Let’s look at the 9 reasons. Actually, reason 9 is a big one.

#1 A poor learning approach

This is kind of funny. We all learn in different ways, but I have noticed that many have an idea that learning to program (Python in our case) is like you remember school. You go to class, learn the skill, and then you master it.

Actually, nobody learns like that. If you don’t use it. There is no way you will learn it.

Another problem with this naive idea, is, that it takes time to process the input. And actually, your brain does an amazing job while you are not consciously thinking about it.

Let me ask you a question. Did you ever sit with problem and try really hard to figure it out. It might be something like remember a name of one of your acquaintances. You think really hard about it, but you fail. You start talking about something else, but then suddenly, while you are focused on something else, the name pops up.

“Jack” you yell out of surprise, while the people around you do not understand what this is about. Well, the name was Jack, the acquaintance you forgot the name of.

Learning Python is the same. You need to let your brain process the new stuff, while you don’t think of it.

Take this free Python course in 17 lessons.

The course is in 17 lessons. Each lesson comes with some theory, and a project, along with a solution to the project.

Said differently:

  • It introduces new concepts for you to understand.
  • Then you are introduced to a small project using the new concepts.
  • Finally, a solution is provided for you to see how it can be solved.

Well, how would I suggest we apply our knowledge to that.

  • Day 1: See lesson with new concepts – take notes.
  • Day 2: Recap lesson (either from notes or video) – then see introduction to project. Try to solve the project and stop when you get stuck.
  • Day 3: Possibly recap lesson again, then continue with project. If you are really stuck – see solution.

This gives your subconscious time to learn and is not overloaded with new concepts.

The full 17 lessons will be 51 days of programming, but done in a way that will leave time for you to really learn it.

#2 Start something different when it gets tough

We used to learn that knowledge is power. When I was a kid the only place you could get knowledge was in school, documentaries in the telly, library books, some magazines, and so forth.

Most of them (all except libraries) were chosen by someone else. If I wanted to learn programming, there was only the library to go to. Because, programming was not in big demand. So there was no programming in school, no documentaries where you could learn programming, no magazines (at least available in my local stores), so there was the library.

Unfortunately, there we not many books on programming and most used programming languages I didn’t have access to.

Today, we live in the Information Age. But that actually has a price.

The information is competing to get our attention.

What do I mean?

Did you ever wonder why you keep scrolling the news feed on your favorite news media? You want to find the most interesting news and you do not want to miss the best ones and be the only one not knowing. You want to be the first one to share it your friends and close ones.

Learning is the same.

If you want to learn Python one week because you want that 6-figure contract they talk about, then when it becomes a big tough, well, you focus on something different, which looks more appealing than suffering the feeling of not being smart enough to learn Python. Why waste energy on that when you can learn something better giving you the live you have been wanting.

How can we use that to our understanding?

Know that the mind will try to convince you to start something else, something that looks easier, when it becomes challenging with learning Python.

There will be times when it doesn’t seem easy to learn Python. I have been sitting with bugs for days, felt hopeless, stupid, not smart enough. But when you solve it, I also know the feeling I will get.

Prepare yourself not to quit and start something else, when it gets tough.

#3 Lack of Focus

This is kind of related to the previous one. With all the possibilities comes difficult choices.

Most learners want to learn everything about everything.

As a senior, you know that is not possible. Most senior developers aer specialists and really good at what they do. That is why they get paid high salaries, not because they are average at everything.

When it comes to learning, the more clear you are on what you want to learn, the easier it is.

Python is a great programming language, which is great at many aspects of programming. But this also opens options to choose from, and as a beginner, you think you need to master them all.

And I understand, that you might not know if you want to make web apps with Python or want to use it for Machine Learning.

But becoming really good at one thing, will make it easier to transition to another programming aspect.

Stay focused on learning one purpose of Python programming. Learn that good. Then switch focus if you figure out something else has your interest.

A focused learning path will teach you programming faster.

#4 Unclear goal

The fastest from first line of code to a paid job that I know of was done of friend of mine. He did it 9 months. Yes, he has a steady salary every month. And he loves what he does.

He used to serve coffee in a coffee-shop (not the types in Amsterdam), and the pay was hopeless, people were rude, working hours awful, his boss was (I think we should sensor what he said here).

How did he manage to get this job that fast?

Well, learning Python programming in 9 month to a level where you get paid, that is remarkable. I am not talking about someone making script to automate trivial things. No, he builds web applications and writes Python backend code. That is great achievement.

What did he do?

He had a clear goal. He wanted to write Python backend code for web applications.

He first of all, stayed focused on only learning that, and also, his goal was clear.

It was not just something he tried to see if he liked it or had talent for. Do you know what I mean? Well, most of the time when we begin something, we only “try it” to “feel if it is something for me”.

That is vague and unclear what that means?

He had a clear goal. Python backend web applications.

Now he does it.

Keep a clear goal.

#5 Poor learning material

Do you remember the hopeless hours you spend in school being bored, counting the seconds until the break?

Luckily, our brain help us remember most positive things. But I do remember all that. What I remember better are the hours at lectures at University, listening to professors that know a great deal about the subject, but where really poor at teaching.

The truth is, most professors at University are more passionate about new science, than teaching students that are not engaging.

If I was to redo my University time, I would not waste time on the lectures. They were not good.

Funny enough, the books were either written by the professors or 600-1000 pages books authored by the sole purpose of getting as many pages as possible.

To take the professors books, well, they had the advantage, that they were more notes and only a few pages for each lecture. The bad part was, they were not good and often not structured very good.

The 600-1000 pages books, were no better, it was pages up and down written with the sole purpose of filing as many pages as possible. Why? Because they get paid by pages, and the ULTIMATE BOOK OF (insert the subject here), just needs to be 700+ pages to convince you.

Again, we want to learn it all. We want the ULTIMATE BOOK, instead of a specialized book.

What is the point?

Poor learning material kills the joy, and learning material often has another agenda than teaching. To sell books and courses.

The same is true of most material online. The main purpose is not making great learning material, it is to sell.

What to do about it?

Find an inspiring source to learn from. Someone that is burning to teach others, not just focused on earning money.

#6 Not realistic time frame

We all hear the story about that guy or girl, which got the first paying job in 9 months and less. And yes, I know it is possible.

But let’s face the truth.

No matter how talented and naturally gifted you are, you will not be on a senior developer level after 9 months.

It takes on average 5 years of full time professional working to become a senior developer. If you take a college degree in computer science, that is, you spent 5 years learning a bunch of programming stuff, then it still takes 5 years of professional working experience to become a senior developer.

Because learning is one thing. Using it in real world is another thing.

But when we hear it can be done in a short time frame, we immediately think we can do the same.

The only person I know who did it in 9 months, well, he committed all his free time to do it. All he did was, work his miserable job, train a bit to stay physically healthy and relax that way, eat and then code, code, code, and some more code.

Most people cannot commit to this kind of life for 9 months. And some people, me included, are not able to do it in 9 month, even if we had the time.

Why?

Because we all have different learning journeys. I always felt like a slow learning, and I most likely am. But you know what? In the end it doesn’t matter how long you used to get there. You will be rewarded in the same way.

How can we use this knowledge?

Well, don’t set goals like “I will get my first paying job in 12 months”. Sorry, only a few can do it, and I will reveal a last thing about my friend.

He did an extraordinary thing to get the job. Not just a normal application and land it. No he tried that with no success. He did more than that. First of all, he called the hiring manager talking to him. Had phone interviews first. Accepted to start with 14 days with no pay and no guarantee to get hired.

How long does it take on average?

Well, I cannot say. It depends on how much work you put in. Just saying, that these wonder stories are not everyday stories.

Other options, like making freelance jobs, is quite easy to get.

#7 Feel alone

For a senior like me, I didn’t think about at first. I started programming at age 12, and got some friends hooked on it as well. Then in college I was surrounded by like minded.

Then I talk to all of you and I realize. Many sit alone with problems and don’t know where to find answers to your problems.

Most don’t feel comfortable asking beginners questions on Stackoverflow, as it is more focused on how to solve specific questions and not on how to understand some programming concepts.

You burn to learn programming, but do not have a teacher to ask.

That makes you feel alone and many give up.

As a senior developer, you find all the answers to your problems on Stackoverflow and discuss it with your colleagues or other friends with similar interests you have gained by working in the tech industry your entire life.

But as someone starting to learn, where can you ask these questions?

  • The ideal is to have a one-on-one tutor, but that is often expensive, especially if you need qualified help.
  • Take a paid course with guaranteed help from the instructor.
  • Ask someone on Twitter (I get asked by people tagging me).
  • Start in group together – have a Facebook group where you study together with others. A place where you can ask questions and get help from others.

#8 Lack of motivation

This is funny. We talked about focus and clear goals, but often something interesting happens. You loose motivation.

Many think that motivation just sticks if you want something bad enough.

Let me ask you, did you ever experience that you quit on a goal that you were really exited about?

Most will agree they did.

Many will also say, I thought I had a clear goal and focused strategy to get there.

So what happened?

This is what we call lack of motivation. It can sneak up unexpected.

What to do to keep motivation?

Print out your goal. Like my friend. “I will become a Python backend web application programmer.”

Put it in a place you see every day, on the mirror in your bathroom. When you see it, read it out loud. Remind yourself how it will feel when you accomplish your goal.

If you do it every day, you will keep the motivation.

#9 Compare yourself to others

I think this one is big.

You sit there with a problem, you see other just solve it, you get the explanation, you still don’t get it, you feel stupid, because you are the only one in the room not getting it.

Or you see others succeed and get jobs in 9 months, you’ve been working on learning Python in 22 months, and you still don’t fully understand the basics.

Well, I have been there and can still there today. You can always find someone to compare yourself that will make you feel stupid.

The good news is, you don’t need to be a rock-star at every aspect of programming. You just need to be decent. We all make, so called rookie-mistakes, as senior develoers. I have done it, my colleagues have done it, we continue to do it. We ask for help, we laugh when we realize how embarrassing and simple the mistake was. But we have done so many times, and know it will happen again.

How to avoid this?

Stop comparing yourself to others. Your learning journey is yours. In the end, it doesn’t matter how you got there.

As a senior developer, no-one would look at your CV as notice you spend an extra year learning something. Because now you know it and they need your skills.

Python is in high demand.

Want to learn Python programming?

If you are hooked on learning Python I will suggest you follow my beginners course on Python.

It is well structured and has focus on you as a learner.

I suggest you break it down as explained in #1.

  • Day 1: See lesson with new concepts – take notes.
  • Day 2: Recap lesson (either from notes or video) – then see introduction to project. Try to solve the project and stop when you get stuck.
  • Day 3: Possibly recap lesson again, then continue with project. If you are really stuck – see solution.

Then continue that pattern for each lesson.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

15 Most Useful pandas Shortcut Methods

What will you learn?

Everybody likes the pandas data structure DataFrame, but most miss out on what powerful methods it provides.

pandas is a huge module, which makes it difficult to master. Most just use the data structure (DataFrame), without utilizing the power of the methods. In this tutorial you will learn the 15 most useful shortcut that will help you when working with data in pandas data structures.

#1 groupby

The groupby method involves some combination of splitting the object, applying a function, and combining the result.

Wow. That sounds complex. But it is not. It can be used to group large amounts of data and compute operations on these groups.

Best way to learn is to see some example.

import pandas as pd

data = {'Items': ['Apple','Orange', 'Pear', 'Orange', 'Apple'], 
        'Price': [12, 5, 3, 7, 24]}
df = pd.DataFrame(data)

This results in this DataFrame.

The groupby method can group the items together, and apply a function. Let’s try it here.

df.groupby(['Items']).mean()

This will result in this output.

As you see, it has grouped the Apples, Oranges, and the Pears together and for the price column, it has applied the mean() function on the values.

Hence, the Apple has value 18, as it is the mean of 12 and 24 ((12 + 24)/2). Similar, for Orange and Pear.

#2 memory_usage()

We get more and more data and our project get bigger and bigger. At one point you will need to analyze how much memory your data is using.

What memory_usage does, is, it returns the memory usage of each column in the DataFrame. Sometimes, the data type of a column is object, what that means is, that it is pointing to another object. To get the data usage of these objects included, you need to use the deep=True argument.

Let’s try both, to see the difference.

import pandas as pd

dtypes = ['int64', 'float64', 'complex128', 'object', 'bool']
data = dict([(t, np.ones(shape=1000, dtype=int).astype(t)) for t in
                                                          dtypes])
df = pd.DataFrame(data)

print(df.head())

Then we can get the memory usage as follows.

print(df.memory_usage())

Giving the following.

Index           128
int64          8000
float64        8000
complex128    16000
object         8000
bool           1000
dtype: int64

Also, with deep=True.

df.memory_usage(deep=True)

Giving the following where you see the object column is uses more space.

Index           128
int64          8000
float64        8000
complex128    16000
object        36000
bool           1000
dtype: int64

#3 clip()

clip() can trim values at the input threshold.

I find this is easiest to understand by inspecting an example.

import pandas as pd

data = {'col_0': [9, -3, 0, -1, 5], 'col_1': [-2, -7, 6, 8, -5]}
df = pd.DataFrame(data)

print(df)

Then we apply the clip, which will ensure the values below -2 are replaced with -2, and values above 5, are replaced with 5. It clips the values.

print(df.clip(-2, 5))

#4 corr()

The correlation between the values in a column can be calculate with corr(). There are different methods to use: Pearson, Kendall, and Spearman. By default it uses the Pearson method, which will do fine giving you an idea if columns are correlated.

Let’s try an example.

import pandas as pd

df = pd.DataFrame([(.2, .3), (.0, .6), (.6, .0), (.2, .1)],
                  columns=['dogs', 'cats'])

The correlation is given by.

print(df.corr())

The value 1.0 is saying it is perfect correlation, which are shown in the diagonal. This makes sense, as the diagonal is the column with itself.

To learn more about correlation and statistics, be sure to check this tutorial out, which also explains the correlation value and how to interpret it.

#5 argmin()

The name argmin is a bit strange. What it does, it returns the position (the index) of the smallest value in a Series (column of a DataFrame).

import pandas as pd

s = pd.Series({'Corn Flakes': 100.0, 'Almond Delight': 110.0,
               'Cinnamon Toast Crunch': 120.0, 'Cocoa Puff': 110.0})

print(s)

Gives.

Corn Flakes              100.0
Almond Delight           110.0
Cinnamon Toast Crunch    120.0
Cocoa Puff               110.0
dtype: float64

And to get the position of the smallest value, just apply the method.

print(s.argmin())

Which will give 0. Remember that it is zero-index, meaning that the first element has index 0.

#6 argmax()

Just like argmin, then argmax() returns the largest element in a Series.

Continue with the example from above.

print(s.argmax())

This will give 2, as it is the largest element in the series.

#7 compare()

Want to know the differences between DataFrames? Then compare does a great job at that.

import pandas as pd
import numpy as np

df = pd.DataFrame(
     {
         "col1": [1.0, 2.0, 3.0, np.nan, 5.0],
         "col2": [1.0, 2.0, 3.0, 4.0, 5.0]
     },
     columns=["col1", "col2"],
)

We can compare the columns here.

df['col1'].compare(df['col2'])

As you see, the only row that differ is the above.

#8 replace()

Did you ever need to replace a value in a DataFrame? Well, it also has a method for that and it is called replace().

df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
                   'B': [5, 6, 7, 8, 9],
                   'C': ['a', 'b', 'c', 'd', 'e']})

Let’s try to replace 5 with -10 and see what happens.

print(df.replace(5, -10))

#9 isna()

Wanted to find missing values? Then isna can do that for you.

Let’s try it.

import pandas as pd
import numpy as np

df = pd.DataFrame(dict(age=[5, 6, np.NaN],
                  born=[pd.NaT, pd.Timestamp('1939-05-27'),
                        pd.Timestamp('1940-04-25')],
                  name=['Alfred', 'Batman', ''],
                  toy=[None, 'Batmobile', 'Joker']))

Then you get the values as follows.

print(df.isna())

I often use it also in a combination with sum(), which will then tell how many rows in each column are missing. This is interesting to get an idea about the quality of the dataset.

print(df.isna().sum())
age     1
born    1
name    0
toy     1
dtype: int64

#10 interpolation()

On the subject of missing values, what to do? Well, there are many options, but one simple can be to interpolate the values.

import pandas as pd
import numpy as np

s = pd.Series([0, 1, np.nan, 3])

This gives the following series.

0    0.0
1    1.0
2    NaN
3    3.0
dtype: float64

Then you can interpolate and get the value between them.

print(s.interpolate())
0    0.0
1    1.0
2    2.0
3    3.0
dtype: float64

This is just one way to deal with it. Dealing with missing values is a big subject. To learn more read this tutorial on the subject.

#11 drop()

Ever needed to remove a column in a DataFrame? Well, again they made a method for that.

Let’s try the drop() method to remove a column.

import pandas as pd

data = {'Age': [-44,0,5, 15, 10, -3], 
        'Salary': [0,5,-2, -14, 19, 24]}
df = pd.DataFrame(data)

Then let’s remove the Age column.

df2 = df.drop('Age', axis='columns')
print(df2)

Notice, that it returns a new DataFrame.

#12 drop_duplicates()

Dealing with data that has duplicate rows? Well, it is a common problem and pandas made a method to easily remove them from your DataFrame.

It is called drop_duplicates and does what it says.

Let’s try it.

import pandas as pd

df = pd.DataFrame({
    'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'],
    'style': ['cup', 'cup', 'cup', 'pack', 'pack'],
    'rating': [4, 4, 3.5, 15, 5]
})

This DataFrame as duplicate rows. Let’s see how they can be removed.

df2 = df.drop_duplicates()
print(df2)

#13 sum()

Ever needed to sum a column? Even with multi index?

Let’s try.

import pandas as pd

idx = pd.MultiIndex.from_arrays([
    ['warm', 'warm', 'cold', 'cold'],
    ['dog', 'falcon', 'fish', 'spider']],
    names=['blooded', 'animal'])
s = pd.Series([4, 2, 0, 8], name='legs', index=idx)
print(s)

This will output.

blooded  animal
warm     dog       4
         falcon    2
cold     fish      0
         spider    8
Name: legs, dtype: int64
In [29]:

Then this will sum the column.

print(s.sum())

And it will output 14, as expected.

#14 cumsum()

Wanted to make a cumulative sum? Then cumsum() does the job for you, even with missing numbers.

import pandas as pd

s = pd.Series([2, np.nan, 5, -1, 0])
print(s)

This will give.

0    2.0
1    NaN
2    5.0
3   -1.0
4    0.0
dtype: float64

And then.

print(s.cumsum())

Gives.

0    2.0
1    NaN
2    7.0
3    6.0
4    6.0
dtype: float64

Where it makes a cumulative sum down the column.

#15 value_counst()

The value_counts() method returns the number of unique rows in a DataFrame.

This requires an example to really understand.

df = pd.DataFrame({'num_legs': [2, 4, 4, 6],
                   'num_wings': [2, 0, 0, 0]},
                  index=['falcon', 'dog', 'cat', 'ant'])

Here we see we have two rows with 4 and 0 (in that order), while the other rows have unique values.

print(df.value_counts())
num_legs  num_wings
4         0            2
2         2            1
6         0            1
dtype: int64

We see there are two rows with 4 and 0, and one of the other rows.

Bonus: unique()

Wanted the unique elements in your Series?

Here you go.

import pandas as pd

s = pd.Series([2, 1, 3, 3], name='A')
print(s.unique())

This will give the unique elements.

array([2, 1, 3])

Want to learn more?

Want to learn more about Data Science to become a successful Data Scientist?

Then check my free Expert Data Science Blueprint course with the following resources.

  • 15 video lessons – covers the Data Science Workflow and concepts, demonstrates everything on real data, introduce projects and shows a solution (YouTube video).
  • 30 JuPyter Notebooks – with the full code and explanation from the lectures and projects (GitHub).
  • 15 projects – structured with the Data Science Workflow and a solution explained in the end of video lessons (GitHub).
Data Science

11 Great List Methods That Most Don’t Know

What will you learn?

After this list you will know the most useful methods and techniques with Python lists. It is funny, that many of these are unknown to many that work with Python. Be sure not to be one of them as they can save you a lot of time.

#1 in

This one is probably my favorite. To check if an element is in the list. This is easy with Python and just why we can’t stop loving it.

this_list = ['foo', 'bar', 'foobar', 'barfoo']

if 'foo' in this_list:
    print('yes')

if 'foofoo' in this_list:
    print('no')

This will only print yes as ‘foofoo‘ is not an element in the list this_list.

On the other hand ‘foo‘ is in the list, hence it prints yes.

#2 sorted()

This is actually a built-in function and it returns a new sorted list from the items in an iterable.

An iterable is a list or anything you can iterate over in Python.

Let’s try it.

this_list = ['foo', 'bar', 'foobar', 'barfoo']

print(sorted(this_list))
print(sorted(this_list, reverse=True))

This will output.

['bar', 'barfoo', 'foo', 'foobar']
['foobar', 'foo', 'barfoo', 'bar']

Hence, it can also sort in reverse order.

It is important to understand that it returns a new list with the items from the input argument.

Want to learn more Built-in functions?

#3 sort()

This is kind of funny. And it might look like the same. But there is a big difference.

The method sort() will sort the list in-place and not return a new list.

Let’s try it.

this_list = ['foo', 'bar', 'foobar', 'barfoo']

this_list.sort()
print(this_list)

This will output.

['bar', 'barfoo', 'foo', 'foobar']

What was the difference? Well, sorted() returns a new list, and sort() sorts the list and changes the order of the element of the original list.

Why does that matter?

There can be many reasons – but one major one is if the list is huge and you need to save space and time – then in-place sorting is both more memory and speed efficient.

#4 for

Again, a favorite of mine. Iterating over a list in Python is such a pleasure. Why make complex syntax for something you need all the time?

Well, in Python it is easy to do.

this_list = ['bar', 'barfoo', 'foo', 'foobar']

for item in this_list:
    print(item)

This will output.

bar
barfoo
foo
foobar

#5 append()

Adding an element to the end of a list is something you need all the time. Again, Python does it in a simple manner.

this_list = ['bar', 'barfoo', 'foo', 'foobar']

this_list.append('foobarfoo')

print(this_list)

This will output.

['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo']

And see the added (appended) element at the end of the list.

#6 concatenate lists

This one is great. Again Python goes the extra mile to make things a simple as possible.

If you need to concatenate two lists together. How is that done?

Well, with the addition sign. See this.

list_a = ['foo', 'bar']
list_b = ['foobar', 'barfoo']

list_c = list_a + list_b

print(list_c)

This will output.

['foo', 'bar', 'foobar', 'barfoo']

#7 index()

Sometimes you need the index of an element in a list.

Well, here you go.

this_list = ['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo']

print(this_list.index('foo'))

This will output 2, as ‘foo‘ has the first occurrence in the list at index 2 (remember, that Python lists are zero indexed – meaning that the first element is 0, second element is 1, etc.).

#8 copy()

Sometimes you need a new copy of a list. Let’s try and understand what that means.

this_list = ['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo']

list_copy = this_list.copy()

list_copy.append('me element')

print(list_copy)
print(this_list)

This will output.

['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo', 'me element']
['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo']

As you see, the append only modifies the list it is appended to. This might not surprise you.

But let’s try something different.

a = this_list

a.append('element')

print(a)
print(this_list)

This will output.

['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo', 'element']
['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo', 'element']

Oh no – the element got appended to both lists – or what?

No, actually it is the same list, it is just two variables, which point to the same list.

Now copy starts to make sense.

#9 remove()

Now what if you need to remove an element form this list?

Let’s try how easy it could be.

this_list = ['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo', 'element']

this_list.remove('element')

print(this_list)

This will output.

['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo']

Oh, it was that simple.

#10 pop()

Actually a Python list can be used as a Stack out of the box.

Append (as we saw above) pushes an element to the back of the list – or you could say, the top of the stack.

And then, what does pop do?

this_list = ['barfoo', 'foo', 'foobar', 'foobarfoo']

element = this_list.pop()

print(element)
print(this_list)

This will output.

foobarfoo
['barfoo', 'foo', 'foobar']

As you see, this is just as a stack.

You might complain, it does not have the same performance (big O) as a real stack. Well, because the list is implemented in such an awesome way, it has asymptotic the the same big-O complexity. What does that mean? That it is just as good as a “real” stack.

#11 del

This is actually one I don’t use that much – but it is handy if you like this notation.

Let’s just try it.

this_list = ['barfoo', 'foo', 'foobar']

del this_list[1]

print(this_list)

This will delete element at position 1 and give this output.

['barfoo', 'foobar']

Wow. That was great.

Want to learn more?

If this is something you like and you want to get started with Python, then check my 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.’
Exit mobile version