Featured Post

SQL Interview Success: Unlocking the Top 5 Frequently Asked Queries

Image
 Here are the five top commonly asked SQL queries in the interviews. These you can expect in Data Analyst, or, Data Engineer interviews. Top SQL Queries for Interviews 01. Joins The commonly asked question pertains to providing two tables, determining the number of rows that will return on various join types, and the resultant. Table1 -------- id ---- 1 1 2 3 Table2 -------- id ---- 1 3 1 NULL Output ------- Inner join --------------- 5 rows will return The result will be: =============== 1  1 1   1 1   1 1    1 3    3 02. Substring and Concat Here, we need to write an SQL query to make the upper case of the first letter and the small case of the remaining letter. Table1 ------ ename ===== raJu venKat kRIshna Solution: ========== SELECT CONCAT(UPPER(SUBSTRING(name, 1, 1)), LOWER(SUBSTRING(name, 2))) AS capitalized_name FROM Table1; 03. Case statement SQL Query ========= SELECT Code1, Code2,      CASE         WHEN Code1 = 'A' AND Code2 = 'AA' THEN "A" | "A

How to Deal With Missing Data: Pandas Fillna() and Dropna()

Here are the best examples of Pandas fillna(), dropna() and sum() methods. We have explained the process in two steps - Counting and Replacing the Null values.


Check and Replace Column Nulls


Count Nulls

## count null values column-wise

null_counts = df.isnull().sum()


print(null_counts)

```


Output:

```

Column1    1

Column2    1

Column3    5

dtype: int64

```

In the above code, we first create a sample Pandas DataFrame `df` with some null values. Then, we use the `isnull()` function to create a DataFrame of the same shape as `df`, where each element is a boolean value indicating whether that element is null or not. Finally, we use the `sum()` function to count the number of null values in each column of the resulting DataFrame. The output shows the count of null values column-wise. to count null values column-wise:


```

df.isnull().sum()

```


##Code snippet to count null values row-wise:


```

df.isnull().sum(axis=1)

```


In the above code, `df` is the Pandas DataFrame for which you want to count the null values. The `isnull()` function returns a DataFrame with the same shape as `df`, where each element is a boolean value indicating whether that element is null or not. 

The `sum()` function is then applied to the resulting DataFrame to count the number of null values.

Fill null values with zeros in Pandas


```

import pandas as pd


# create a sample dataframe

data = {'Column1': [1, 2, 3, 4, None],

        'Column2': ['A', 'B', None, 'C', 'D'],

        'Column3': [None, None, None, None, None]}

df = pd.DataFrame(data)


Fill Nulls

To fill null values with '0' in Pandas DataFrame, you can use the `fillna()` function. Here's an example code snippet to do this:


```

import pandas as pd


# create a sample dataframe

data = {'Column1': [1, 2, 3, 4, None],

        'Column2': ['A', 'B', None, 'C', 'D'],

        'Column3': [None, None, None, None, None]}

df = pd.DataFrame(data)


# fill null values with 0

df.fillna(0, inplace=True)


print(df)

```


Output:


```

   Column1 Column2  Column3

0      1.0      A      0.0

1      2.0      B      0.0

2      3.0      0      0.0

3      4.0      C      0.0

4      0.0      D      0.0

```

In the above code, we first create a sample Pandas DataFrame `df` with some null values. Then we use the `fillna()` function to replace all null values in the DataFrame with '0'. The `inplace=True` parameter ensures that the original DataFrame is modified and not a copy. Finally, we print the modified DataFrame with null values filled with '0'.


Note that the `axis` parameter is set to 0 by default in the `sum()` function, which means that it counts null values column-wise. To count null values row-wise, you need to set `axis` to 1.


Drop Nulls


df.dropna() 

It drops rows with any columns having the Nulls.

Comments

Popular posts from this blog

How to Fix datetime Import Error in Python Quickly

Explained Ideal Structure of Python Class

How to Check Kafka Available Brokers