Python
- 14 February
Merge Multiple CSV Files in Python
Using pd.concat() Method To merge multiple CSV files in Python: Create a list containing all CSV files we will merge. Create an empty dataframe. Use the for loop to iterate over the files. In each iterate, use read_csv() to read CSV files and concatenate using the pd.concat() method. Use to_csv() to write merged data into […]
- 13 February
Count Files in Directory in Python
Using os Module We can use the listdir(), scandir(), and walk() methods of the os module to count files in the current directory, including/excluding sub-directories. Before diving into the details, let’s look at the directory structure we will use for this article; you may use your own. E:\Test – contains two text files (File1.txt and […]
- 27 December
Create Array of All NaN Values in Python
Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-678e2de7b9454790446441/] [crayon-678e2de7b9458155879898/] We used numpy.empty() to get an array of the given shape and dtype. Here, shape means the number of […]
- 27 December
Call Function from Another Function in Python
1. Introduction In Python, calling a function from another function is a common practice that enhances modularity and reusability. We’ll explore various ways to do this, with complete examples and detailed explanations. Our focus will be on understanding how these methods work, their use cases. As an example, let’s look at a scenario where we […]
- 27 December
[Fixed] NameError Name ‘unicode’ is Not Defined in Python
Use str() Method To resolve the NameError: name 'unicode' is not defined, replace the occurrence of unicode() with str(). This issue occurs because unicode() was renamed to str() in Python 3. [crayon-678e2de7b9ced337775295/] [crayon-678e2de7b9cf2613597753/] Unicode is a computing industry standard that ensures that text from most of the world’s writing systems is consistently encoded, rendered, and […]
- 26 December
Repeat List N Times in Python
This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and this will repeat list 3 times. See the code below. [crayon-678e2de7ba2c2518502816/] Output: [2, 4, 6, 11, […]
- 26 December
Get First Day of Next Month in Python
Get First Day of Next Month in Python This tutorial will demonstrate how to get first day of next month in Python. Using the datetime.replace() with datetime.timedelta() function To get first day of next month in Python: Use the datetime object to store the current date and time. Use the datetime.replace() and datetime.timedelta() function to […]
- 25 December
Convert String Array to Int Array in Python
This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop through the array. Use the int() function to convert each element to an integer in every iteration. See the […]
- 25 December
Encode String to UTF-8 in Python
The UTF-8 encoding is used by default in Python and represents 8-bit Unicode values. The upgrade to Python 3 saw a major change in using ASCII characters to Unicode characters by default for strings. Encode String to UTF-8 in Python This tutorial will demonstrate how to encode string to UTF-8 in Python. Using the encode() […]