string-concatenation

Row wise concatenation and replacing nan with common column values

Row wise concatenation and replacing nan with common column values Question: Below is the input data df1 A B C D E F G Messi Forward Argentina 1 Nan 5 6 Ronaldo Defender Portugal Nan 4 Nan 3 Messi Midfield Argentina Nan 5 Nan 6 Ronaldo Forward Portugal 3 Nan 2 3 Mbappe Forward France …

Total answers: 2

How get string between expression with Pandas?

How get string between expression with Pandas? Question: I would like to get only codes between # and concat it to a new column. What I have id code 0 (#M05Q01900R00100# = 1) AND (#M05Q01950R00200# = 0) 1 (#M05Q01900R00100# = 1) AND ((#M05Q01950R00100# = 0) OR (#M05Q01950R00200# = 0)) 2 (#M05Q01600R00100# = 1) 3 (#M05Q01125R00200# …

Total answers: 1

Support string concatenation when using the Str classes' dunder method

Support string concatenation when using the Str classes' dunder method Question: Consider a class which supports cast to string, and supports concatenation (Python add) when the second operand is a string: $ cat dunder.py class Foo: def __str__(self): return "foo" def __add__(self, second): return str(self) + str(second) f = Foo() print(f) print(f + "bar") print("bar" …

Total answers: 1

Concatenate and shuffle entries in a list of strings in Python

Concatenate and shuffle entries in a list of strings in Python Question: Suppose I have a list (or a NumPy array) of strings, e.g., X = [‘abc’, ‘def’, ‘ghi’, ‘jkl’], a list of indices, e.g., J = [0, 3], and a random permutation p, e.g., [0, 3, 2, 5, 1, 4]. I want to find …

Total answers: 2

How can I remove whitespace from concatinated substring indexing in Python?

How can I remove whitespace from concatinated substring indexing in Python? Question: I’m working on a problem for a CS intro class. Though I’ve gotten it to work, I haven’t gotten it to work properly. This is the assignment: Write a program which reads a string using input(), and outputs the same string but with …

Total answers: 2

psycopg2 difference between AsIs and sql module

psycopg2 difference between AsIs and sql module Question: To choose dynamically a table name in a query I used to use AsIs() from psycopg2.extensions ( http://initd.org/psycopg/docs/extensions.html#psycopg2.extensions.AsIs ), with the following syntax: cur.execute(“SELECT * FROM %s WHERE id = %s;”, (AsIs(‘table_name’), id)) However, the documentation now recommends to use the new psycopg2.sql module available in version …

Total answers: 1

Is python += string concatenation bad practice?

Is python += string concatenation bad practice? Question: I am reading The Hitchhiker’s Guide to Python and there is a short code snippet foo = ‘foo’ bar = ‘bar’ foobar = foo + bar # This is good foo += ‘ooo’ # This is bad, instead you should do: foo = ”.join([foo, ‘ooo’]) The author …

Total answers: 2

How to print an incremented int identifier in a string in Python

How to print an incremented int identifier in a string in Python Question: In Java, If I want to print an incremented int variable, I can do it as: int age = scn.nextInt(); System.out.println(“You are ” + age + ” years old going on ” + (age+1)); Output: 21 You are 21 years old going …

Total answers: 3

Is the time-complexity of iterative string append actually O(n^2), or O(n)?

Is the time-complexity of iterative string append actually O(n^2), or O(n)? Question: I am working on a problem out of CTCI. The third problem of chapter 1 has you take a string such as ‘Mr John Smith ‘ and asks you to replace the intermediary spaces with %20: ‘Mr%20John%20Smith’ The author offers this solution in …

Total answers: 4

String concatenation without '+' operator

String concatenation without '+' operator Question: I was playing with python and I realized we don’t need to use ‘+’ operator to concatenate static strings. But it fails if I assign it to a variable. For example: string1 = ‘Hello’ ‘World’ #1 works fine string2 = ‘Hello’ + ‘World’ #2 also works fine string3 = …

Total answers: 6