Counting the number of 1’s that appear in a pandas string

I have a dataset that looks like this: I’d like to count the occurrence of ones for each case, such that the final output is number of 1’s per id. In the final output, id 115 has zero 1’s while id 143 has one. I had an idea of splitting the string into six columns, and then summing that up, but that sounded very inefficient, besides that I failed to implement it. Help is always appreciated. Answer Using DataFrame.assign, Series.str.count and groupby.sum: I assumed that my_var column is string type, if not, replace: With

How to add current date with string like ‘1 hour and 5 second ‘ in python

I have to add datetime lik ‘2019-10-18 11:46:08 ‘+ ‘5 hour 5 second’, 5 hour 5 second is coming from the database and this is not fixed what will be it ie time will be like (15 min, 1 hour 15 min etc) i have tried many thing but getting error how to solve this Answer Firstly store separately the datetime string and the offset you want to add to the datetime: Parse the datetime and offset strings: Add the offset to the datetime as a timedelta:

How to extract specific line in text file

I am text mining a large document. I want to extract a specific line. I want to extract the description immediately under ITEM DESCRIPTION. I have tried many unsuccessful attempts. My latest attempt was: But it did not find the text. Is there a way to find ITEM DESCRIPTION and get the line after it or something similar? Answer The following function finds the description on the line below some given pattern, e.g. “ITEM DESCRIPTION”, and also ignores any blank lines that may be present in between. However, beware that the function does not handle the special case when the

How do you scan characters in multi-character multi-string arrays in Python?

I’m working on my first Python project for a device that reads a string from OCR and outputs braille. The braille device can only output 6 letters at a time. I’m stuck trying to scan each character in the 6 character long multi-string array. For simplicity’s sake, for now I only want to print “this is (insert character)” for every character in the multi-string array. In reality, the output would be to run the code that tells the first two motors to create the character in braille, and then do it for the remaining 5 characters with the 10 remaining

Finding Subarrays of Vowels from a given String

You are given a string S, and you have to find all the amazing substrings of S. Amazing Substring is one that starts with a vowel (a, e, i, o, u, A, E, I, O, U). Input The only argument given is string S. Output Return a single integer X mod 10003, here X is number of Amazing Substrings in given string. Constraints 1 <= length(S) <= 1e6 S can have special characters Example I have implemented the following algo for the above Problem. Above Solution works fine for strings of normal length but not for greater length. For example,

How can I check if character in a string is a letter? (Python)

I know about islower and isupper, but can you check whether or not that character is a letter? For Example: Is there any way to just ask if it is a character besides doing .islower() or .isupper()? Answer You can use str.isalpha(). For example: Output:

How can I check if a string contains ANY letters from the alphabet?

What is best pure Python implementation to check if a string contains ANY letters from the alphabet? Where string_1 would return False for having no letters of the alphabet in it and string_2 would return True for having letter. Answer Regex should be a fast approach:

Convert ASCII chars to Unicode FULLWIDTH latin letters in Python?

Can you easily convert between ASCII characters and their Asian full-width Unicode wide characters? Like: to Answer Those “wide” characters are named FULLWIDTH LATIN LETTER: They have range 0xFF00 – -0xFFEF. You can make look-up table or just add 0xFEE0 to ASCII code.

Titlecasing a string with exceptions

Is there a standard way in Python to titlecase a string (i.e. words start with uppercase characters, all remaining cased characters have lowercase) but leaving articles like and, in, and of lowercased? Answer There are a few problems with this. If you use split and join, some white space characters will be ignored. The built-in capitalize and title methods do not ignore white space. If a sentence starts with an article, you do not want the first word of a title in lowercase. Keeping these in mind: