Python
- 04 December
Convert 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 [crayon-678e2dd755aec594739359/] The code above will print the following output […]
- 30 November
Split 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. [crayon-678e2dd755df3317418526/] This code will display the following results. [crayon-678e2dd755df7609709454/] While using list comprehension […]
- 29 November
Prefix 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 November
How 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 November
Skip Iterations in Python loop
💡TL;DR To skip iterations in Python loop, use continue statement. [crayon-678e2dd756c3e923422177/] Looping is one of the fundamentals in Python with one or more types of loops occurring in almost every code. The for loop and the while loop allow the reiteration of tasks in a super-efficient way. However, there are various scenarios in which we […]
- 28 November
Convert 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. [crayon-678e2dd75702b120636000/] Output: C:\temp\tempFile.txt We used the Path() constructor to convert String to Path. This constructor accepts a string that contains a required path and converts the string to a POSIX path […]
- 28 November
Get 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. [crayon-678e2dd7570eb031609086/] Output: ava2Blo In the above example, we found the position of characters J and g using the find() function […]
- 28 November
Check 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 November
Remove 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 […]