And make your life easier

Writing Python code is easy at the beginning, but as you add more libraries to your toolkit, your script might get unnecessary lines of code, become long, and look messy. Although the code could still get the job done, it would give you some headaches in the long run.

I know this first hand after cleaning, wrangling, and doing data analysis in Python many times. Fortunately, my Python code has improved throughout the years and, in this article, I will share with you 7 tips to level up your Python code and make your life easier when using Python for data science.

This covers things we do on a daily basis like modifying values in a Pandas dataframe, concatenating strings, reading files, and more!

Let’s say we have the following df dataframe.

data = [[1,2,3], [4,5,6], [7,8,9]]
df = pd.DataFrame(data, columns=[0,1,2])
IN[1]: print (df)OUT[1]: 0 1 2
0 1 2 3
1 4 5 6
2 7 8 9

Now for some reason, you need to add the value ‘01’ to the numbers in column 0. A common approach will be defining a function that does this task and then using the apply function to modify the values of a column.

def add_numbers(x):
return f'{x}01'
df[0] = df[0].apply(add_numbers)IN[1]: print (df)OUT[1]: 0 1 2
0 101 2 3
1 401 5 6
2 701 8 9

This isn’t complicated, but creating a function for every change you need to make in a dataframe is not practical. This is when lambda comes in handy.

The lambda function is similar to the regular Python function but it can be defined without a name, which makes it a nice looking one-liner. The code used before can be reduced with the following.

df[0] = df[0].apply(lambda x:f'{x}01')

Lambda becomes extremely useful when you don’t know whether it’s possible to access an attribute of a series to modify the data.

Let’s say column 0 contains letters and we want to capitalize them.

# if you know the existence of the .str, you can do it this way
df[0] = df[0].str.title()
# if you don't know about .str, you can still capitalize with lambda
df[0] = df[0].apply(lambda x: x.title())

String concatenation is a very common operation in Python and it can be done in different ways. The most common way is to use the + operator; however, a big issue with this operator is that we can’t add any separator between strings.

Of course, if you want to concatenate “Hello” and “World,” a typical workaround is to add a whitespace separator (“ “).

print("Hello" + " " + "World")

That gets…

Continue reading: https://towardsdatascience.com/7-tips-to-level-up-your-python-code-for-data-science-4a64dbccd86d?source=rss—-7f60cf5620c9—4

Source: towardsdatascience.com