Showing posts with the label Dictionary

Featured Post

How to Check Column Nulls and Replace: Pandas

Here is a post that shows how to count Nulls and replace them with the value you want in the Pandas Dataframe. We have explained the process in two steps - Counting and Replacing the Null values. Count null values (column-wise) in Pandas ## 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 Panda

How to Access Dictionary Key-Value Data in Python

Use for-loop to read dictionary data in python. Here's an example of reading dictionary data. It's helpful to use in real projects. Python program to read dictionary data yearly_revenue = {    2017 : 1000000,    2018 : 1200000,    2019 : 1250000,    2020 : 1100000,    2021 : 1300000,  } total_income = 0 for year_id in yearly_revenue.keys() :   total_income+=yearly_revenue[year_id]   print(year_id, yearly_revenue[year_id]) print(total_income) print(total_income/len(yearly_revenue)) Output 2017 1000000 2018 1200000 2019 1250000 2020 1100000 2021 1300000 5850000 1170000.0 ** Process exited - Return Code: 0 ** Press Enter to exit the terminal Explanation The input is dictionary data. The total revenue sums up for each year. Notably, the critical point is using the dictionary keys method. References Python in-depth and sample programs