assign

Any simpler way to assign multiple columns in Python like R data.table :=

Any simpler way to assign multiple columns in Python like R data.table := Question: I’m wondering if there’s any simpler way to assign multiple columns in Python, just like the := in R data.table. For example, in Python I would have to write like this: df[‘Col_A’] = df.A/df.B df[‘Col_B’] = df.C/df.D df[‘Col_C’] = df.E/df.F * …

Total answers: 3

pandas assign with new column name as string

pandas assign with new column name as string Question: I recently discovered pandas "assign" method which I find very elegant. My issue is that the name of the new column is assigned as keyword, so it cannot have spaces or dashes in it. df = DataFrame({‘A’: range(1, 11), ‘B’: np.random.randn(10)}) df.assign(ln_A=lambda x: np.log(x.A)) A B …

Total answers: 2

Nested dictionary in Python: Assigning keys, BUT keeping sub structure

Nested dictionary in Python: Assigning keys, BUT keeping sub structure Question: I am using Python 3.5 and I have a problem assigning values to a dictionary key. The structure of my dictionary looks like this: dict_var = {‘file’: {‘index’: {‘flag’: {‘flag_key’: ‘False’}, ‘attr’: {‘attr_key’: ‘attr_val’}, ‘path’: {‘path_key’: ‘path_val’}, } } } I get the KeyError: …

Total answers: 2

Python assign value and use on same line

Python assign value and use on same line Question: I was trying to create a shortest-possible code for a puzzle, and the question came to mind trying to do something like this: zip(l=[1,2,3,4,5],l[1:]) So I was wondering, is there a way to produce a value, assign it to a variable and use that variable on …

Total answers: 5