Showing posts with label Bigdata. Show all posts
Showing posts with label Bigdata. Show all posts

Wednesday, 26 July 2023

REMOVE EMPTY ROWS, NULL VALUES, MEAN, MEDIAN, MODE - PYTHON

REMOVE EMPTY ROWS

import pandas as pd

df = pd.read_csv('data.csv')

new_df = df.dropna()

print(new_df.to_string())


By default, the dropna() method returns a new DataFrame, and will not change the original.

If you want to change the original DataFrame, use the inplace = True argument

import pandas as pd

df = pd.read_csv('data.csv')

df.dropna(inplace = True)

print(df.to_string())

REPLACE NULL VALUES WITH THE NUMBER 130

import pandas as pd

df = pd.read_csv('data.csv')

df.fillna(130, inplace = True)

REPLACE ONLY FOR SPECIFIED COLUMNS

import pandas as pd

df = pd.read_csv('data.csv')

df["Calories"].fillna(130, inplace = True)

REPLACE USING MEAN, MEDIAN, OR MODE


MEAN = THE AVERAGE VALUE (THE SUM OF ALL VALUES DIVIDED BY NUMBER OF VALUES).


import pandas as pd

df = pd.read_csv('data.csv')

x = df["Calories"].mean()

df["Calories"].fillna(x, inplace = True)

MEDIAN = THE VALUE IN THE MIDDLE, AFTER YOU HAVE SORTED ALL VALUES ASCENDING.

import pandas as pd

df = pd.read_csv('data.csv')

x = df["Calories"].median()

df["Calories"].fillna(x, inplace = True)

MODE = THE VALUE THAT APPEARS MOST FREQUENTLY

import pandas as pd

df = pd.read_csv('data.csv')

x = df["Calories"].mode()[0]

df["Calories"].fillna(x, inplace = True)

Sunday, 22 January 2023

Python - Collections (Arrays)

Collections (Arrays)

There are 4 built-in data types in Python used to store collections of data.

List, Tuple, Set, and Dictionary, all with different qualities and usage.

  • List is a collection which is ordered and changeable. Allows duplicate members.
  • Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
  • Set is a collection which is unordered, unchangeable#, and unindexed. No duplicate members.
  • Dictionary is a collection which is ordered## and changeable. No duplicate members.


#Set items are unchangeable, but you can remove items and add new items.

 

##As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.


List

  • Lists are used to store multiple items in a single variable. 
  • Lists are created using square brackets. 
  • List items are ordered, changeable, and allow duplicate values. 
  • List items are indexed, the first item has index [0], the second item has index [1] etc. 
  • When we say that lists are ordered, it means that the items have a defined order, and that order will not change. 
    • If you add new items to a list, the new items will be placed at the end of the list. 
  • The list is changeable, meaning that we can change, add, and remove items in a list after it has been created. 
  • A list can contain different data types.
  • From Python's perspective, lists are defined as objects with the data type 'list'    


Tuple

  • A tuple is a collection which is ordered and unchangeable. 
  • When we say that tuples are ordered, it means that the items have a defined order, and that order will not change. 
  • Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created. 
    • But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.

Sunday, 11 December 2022

Tuple

 

<class 'tuple'> <class 'str'>
('apple', 'kiwi', 'cherry')
('apple', 'banana', 'cherry', 'orange')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [2], line 32
     30 thistuple = ("apple", "banana", "cherry")
     31 del thistuple
---> 32 print(thistuple)

NameError: name 'thistuple' is not defined

green = apple yellow = banana red = cherry Approach 1 : apple Approach 1 : banana Approach 1 : ['cherry', 'strawberry', 'raspberry'] Approach 2 : apple Approach 2 : ['mango', 'papaya', 'pineapple'] Approach 2 : cherry
2

List

MyList : ['apple', 'banana', 'cherry'] ['apple', 'banana', 'cherry', 'apple', 'cherry']
3
banana ['cherry', 'orange', 'kiwi'] ['apple', 'banana', 'cherry', 'orange'] ['cherry', 'orange', 'kiwi', 'melon', 'mango']
Yes, 'apple' is in the fruits list
['apple', 'blackcurrant', 'cherry']
['apple', 'banana', 'cherry', 'orange']
['apple', 'orange', 'banana', 'cherry']
['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']
['apple', 'cherry']
['apple', 'cherry']
['apple', 'banana']
[]
apple banana cherry
apple banana cherry
apple banana cherry
['apple', 'banana', 'mango']
['banana', 'kiwi', 'mango', 'orange', 'pineapple'] [23, 50, 65, 82, 100] ['pineapple', 'orange', 'mango', 'kiwi', 'banana'] [100, 82, 65, 50, 23]
[50, 65, 23, 82, 100]
['apple', 'banana', 'cherry'] ['apple', 'banana', 'cherry']
1 2

Monday, 5 December 2022

Python String Operations - 9

# STRIN LSTRIP & RSTRIP

of all fruits banana is my favorite of all fruits banana is my favorite

Python String Operations - 8

0
-1
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In [26], line 12
      9 txt = "Hello, welcome to my world."
     11 print(txt.find("q"))
---> 12 print(txt.index("q")) 

ValueError: substring not found


Kiro - Core Features

What is Kiro Kiro is an innovative AI-powered IDE that revolutionizes software development through intelligent assistance and structured wor...