Monday, May 29, 2023

Strings in-built or predefined methods

All objects in Python whether strings, tuples, lists, and so on have some inbuilt methods that they are associated with. People often get confused between a method and a function and many think that it is one and the same thing but the fact is that in Python there is a difference between the methods and functions. Functions have parenthesis and arguments and when these functions get associated with an object they become a method.

Few interesting features:

Whenever, you type a dot ‘.’ operator in front of an object, the idle displays all the methods that can be associated with it. 



You will initially work with the following:

help()

find()

upper()

lower()

strip()

replace()

split()

join()

in and not in (These are membership operator and not methods)

endswith()

You can use dir(str) method to see all the methods associated with string objects.


1. help()

To find complete information about any method use help() function.


The help() function applies to all Python objects.

2. find() 

The find() method will return the lowest index in the string where the desired substring exists.

S.find(sub[, start[, end]])

Where, S is the string, sub is the substring that you are looking for. Arguments in the square brackets are not mandatory.

>>>#find()

>>> x = 'Last Section of the Chapter'

>>> x.find('ast')

1

>>>

3. upper()

This method converts the entire string into upper case.

>>>#upper()

>>> x = 'Last Section of the Chapter'

>>> x.upper()

'LAST SECTION OF THE CHAPTER'

>>>

4. lower()

This method converts entire string into lowercase.

>>>#lower()

>>> x = 'Last Section of the Chapter'

>>> x.lower()

'last section of the chapter'

>>>

5. strip()

This method removes whitespaces or a particular character from a string.

>>> #strip()

>>> money = '$100'

>>> money.strip('$')

'100'

6. replace()

This method removes a character or substring in a string with some other character or string.

>>> #replace()

>>> str1 = 'Happy Chrsitmas'

>>> str1.replace('Happy','Merry')

'Merry Chrsitmas'

>>>

7. split()

This splits a string into a list data type based on the character that is passed as an argument.

>>> #split()

>>> x = 'Last Section of the Chapter'

>>> x.split()

['Last', 'Section', 'of', 'the', 'Chapter']

>>> ip ='222:222:0:02'

>>> ip.split(':')

['222', '222', '0', '02']

>>>

8. join()

Opposite of string.

>>> #join()

>>> student = ['Alex','32','Physics Major', 'Baseball']

>>> ('|').join(student)

'Alex|32|Physics Major|Baseball'

>>>

9. in and not in

Checks if a substring is a part of the string or not.

>>> #in

>>> x = 'Merry Christmas'

>>> 'Meery' in x

False

>>> 'Merry' in x

True

>>> #not in

>>> 'year' not in x

True

>>> 'Christmas' not in x

False

>>>

10. endswith()

This method is used to check if the string ends with a particular string or not.

>>> #endswith()

>>> x = 'Merry Christmas'

>>> x.endswith()

>>> x.endswith('as')

True

>>>

Share:

Wednesday, May 3, 2023

Converting Unstructured Data to Structured Form

As a data scientist, you not only need to fetch the data but also analyze it. Storing the data in a structured form simplifies this task. In this section, we will learn how to convert the data fetched from MongoDB into a structured format.

Storing into a Dataframe

The find function returns a dictionary from a MongoDB collection. You can directly insert it into a dataframe. First, let’s fetch 100 MongoDB documents and then we will store these documents into a dataframe:

import pandas as pd

samples=table.find().sort("_id",pymongo.DESCENDING)[:100]

df=pd.DataFrame(samples)

df.head()


The readability of this dataframe is far better than that of the default format returned by the function.

Writing to a File


Pandas dataframes can directly be exported into CSV, Excel or SQL. Let us try to store this data to a CSV file:

df.to_csv('StructuredData.csv',index=False)

Similarly, you can use the to_sql function to export the data into a SQL database.
Share: