Python
04 DecemberConvert Pandas Dataframe Column to List
Use Series.values.tolist() Method To convert pandas dataframe column to list: Use pd.DataFrame() to read position_salaries as a pandas data frame. Use df["Position"] to get the column position from df Use position.values to get values of the position Use position_values.tolist() to get list of position_values as position_list import pandas as pd # Create a pandas data […]
30 NovemberSplit Integer into Digits in Python
Using List Comprehension To split integer into digits in Python: Use the str() to transform the specified integer to a string. Use a list comprehension to loop over converted String. Use int() to convert every substring to an integer in each iteration. number = 243689 list_of_digits = [int(i) for i in str(number)] print(list_of_digits) This code […]
29 NovemberPrefix b Before String in Python
Prefix b Before String in Python Prefix b before String denotes a byte String. By putting b before String, you can convert String to bytes in Python. The upgrade from Python 2 to Python 3 was considered a major change as many new features were introduced and quite a few changes were done to the […]
29 NovemberHow to Log to Stdout in Python
Use logging.basicConfig() to log to stdout To log to stdout in Python: Specify the format in which we want to have all logs. Use the helper function .basicConfig() to perform basic logging for the logging system. Use .getLogger() to create a logger with the specified name (if it is); otherwise, it will create a root […]
29 NovemberSkip Iterations in Python loop
💡TL;DR To skip iterations in Python loop, use continue statement. x = 0 for x in range(3): if x == 1: continue #Use of the continue statement print('Working on ' + str(x)) ##Output: # Working on 0 # Working on 2 Looping is one of the fundamentals in Python with one or more types of […]
28 NovemberConvert String to Path in Python
Using pathlib library [Python 3.4+] Use Path class’s constructor to convert String to Path in Python. You need to import Path class from pathlib. from pathlib import Path s = "C:/temp/tempFile.txt" path = Path(s) print(path, type(path)) Output: C:\temp\tempFile.txt We used the Path() constructor to convert String to Path. This constructor accepts a string that contains […]
28 NovemberGet String Between Two Characters in Python
Using the string-slicing technique To get String between two characters in Python: Use String’s find() method to find indices of both the characters. Use String’s slicing to get String between indices of the two characters. def getSubstringBetweenTwoChars(ch1,ch2,str): return s[s.find(ch1)+1:s.find(ch2)] s = 'Java2Blog' s2= getSubstringBetweenTwoChars('J','g',s) print(s2) Output: ava2Blo In the above example, we found the position […]
28 NovemberCheck if Date is Greater than Today in Python
Use Comparison Operator with now() To check if the specified date is greater than today in Python: Use .datetime.now() to get the current local time and date. Create a datetime object using datetime.datetime() with the specified date and time. Use the greater than(>) operator with if-else to assess if the given date is greater than […]
27 NovemberRemove Punctuation from List in Python
Use for loop with replace() Method To remove punctuation from list in Python: Use the for loop to iterate over each word (element) of given list. Use a nested loop to iterate over each character in the word. Use the if statement to check if the current character is in string.punctuation, if it is use […]