Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
103 views

Python Cheat Sheet-1

The document provides information on various Python functions and operations for working with data types, strings, dictionaries, and CSV files. It includes functions for typecasting, arithmetic operations, string manipulation, accessing dictionary values, and loading/manipulating data frames from CSV files. Methods are presented for handling errors while loading CSV files and parsing dates, strings, and numeric values.

Uploaded by

Revathy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

Python Cheat Sheet-1

The document provides information on various Python functions and operations for working with data types, strings, dictionaries, and CSV files. It includes functions for typecasting, arithmetic operations, string manipulation, accessing dictionary values, and loading/manipulating data frames from CSV files. Methods are presented for handling errors while loading CSV files and parsing dates, strings, and numeric values.

Uploaded by

Revathy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Function code

Data type and typecast


Datatype of X type(x)
Type cast x to float float(x)
Arithmetic operations
Divide variable 'a' by 5 and assign the value to 'a' a=a/5
Divide variable 'a' by 5 and assign the remainder value to 'a' a=a%5
3 Raise 'a' by 2 and assign the result to 'a' a=a**2

import math
math.pow(x,y)
Access Math inbuilt functions math.sqrt(x)
String Manipulation
Print first character of string s print(s[0]) —forward index
Length of string s len(s)
Print the second last character using backward index print(s[-2])
Print alternating characters in s print(s[::2])
Print s in reverse print(s[::-1])
string to UPPER/LOWER case s.upper(), s.lower()
Split the sentence when there is ‘,’ s.split(',')
Eg

math.pow(x,y)
math.sqrt(x)
Dictionary
A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with

Given dictionary
qualifier_2 = {'KKR': {'Chris Lynn': 4, 'Sunil Narine': 10, 'Gautam Gambhir (c)': 12, 'Robin Uthappa (wk)': 1, 'Is
'Suryakumar Yadav': 31, 'Piyush Chawla': 2, 'Nathan Coulter-Nile': 6, 'Umesh Yadav': 2, 'Ankit Rajpoot': 4, 'Ext
Simmons': 3, 'Parthiv Patel (wk)': 14, 'Ambati Rayudu': 6, 'Rohit Sharma (c)': 26, 'Krunal Pandya': 45, 'Kieron
KKR and MI are keys and others are values
Funtion
Access a value when you know the key

Print all key names in the dictionary, one by one:


Print all values in the dictionary, one by one:

Loop through both keys and values, by using the it


length of dictionary
Adding an item to the dictionary
is done by using a new index key and assigning a
value to it:
delete an item
removes the last inserted item

Delete from dictionary


delete the dictionary
empty the dictionary

Convert a dictionary to data frame


ered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values.

unil Narine': 10, 'Gautam Gambhir (c)': 12, 'Robin Uthappa (wk)': 1, 'Ishank Jaggi': 28, 'Colin Grandhomme': 0,
: 2, 'Nathan Coulter-Nile': 6, 'Umesh Yadav': 2, 'Ankit Rajpoot': 4, 'Extra runs': 7, 'Total batted': 10}, 'MI': {'Lendl
mbati Rayudu': 6, 'Rohit Sharma (c)': 26, 'Krunal Pandya': 45, 'Kieron Pollard': 9, 'Extra runs': 7, 'Total batted': 6}}
es
code
qualifier_2["KKR"]

for i in qualifier_2:
print(i)
print(x)

for x, y in qualifier_2.items():
print(x, y)
len(qualifier_2)

qualifier_2["mpp"]={'Chris Lynn': 4, 'Sunil Narine': 10, 'Gautam Gambhir (c)': 12}


qualifier_2.pop("mpp")
qualifier_2.popitem()
for i in qualifier_2:
del(qualifier_2[i]['Total batted'])
OR del qualifier_2['KKR']
del qualifier_2
qualifier_2.clear()
df=(pd.DataFrame(qualifier_2))
e keys and values.

dhomme': 0,
0}, 'MI': {'Lendl
Total batted': 6}}

Eg

x---keys
y---values
ideal way of loading text data with white spaces as a delimiter
parameter would you use if you want to load data from line no 5 of the csv
load a csv file into pandas data frame with a different set of column names (‘a’,’b’,’c’)?
convert a pandas data frame to csv format?

error handling while loading a csv into data frame


read a nested json into a pandas data frame
output of pd.read_html
Error handling fo UnicodeDecodeError: ‘utf-8’ codec can’t decode byte
read_csv to automatically read dates in datetime format
read a csv with numeric values separated by a comma (eg: 10,000) as integers
pd.read_fwf
pd.read_csv(skiprows = [0,1,2,3,4])
pd.read_csv(header = None, names = [‘a’,’b’,’c’])
pd.to_csv()
pd.read_csv('file.csv', error_bad_lines=False)
pd.read_csv('file.csv', skiprows=[99])
pd.io.json.json_normalize()
List of dataframe objects
pd.read_csv(encoding = )
pd.read_csv(parse_dates = True)
pd.read_csv(thousands = ‘,’)
39786
2749

You might also like