Find the most recent data available and the year it corresponds to
I have a data set containing an index for different years and countries. It's a csv and I read it using Pandas.
There are a lot of missing values for the index, so I want to create new columns with the most recent data available and the year it corresponds to. I have no idea how to code that in Python.
See also questions close to this topic

How to build a layer that merges two images, in an order determined by a third parameter
I want a layer that takes three tensors as input: Two (n,m,k) tensors and one (1) tensor, i.e. one single number. The output should be a (n,m,2k) tensor, achieved by simply having the first k channels be one image and the remaining being the other. Now, the catch is that the order in which we merge them  do we put image one on top of image two or vice versa  should be determined by whether the third input is greater than 0 or not.
By my thinking, this is an entirely static layer without any trainable parameters, so I tried doing the ordering choice with a Lambda layer, as follows:
def image_scrambler(inp): #inp = [im1, im2, aux_input] im1, im2, aux_input = inp[0],inp[1],inp[2] assert aux_input==1 or aux_input==0 if aux_input==0: return [im1, im2] else: return [im2,im1] paired_images = Lambda(image_scrambler)([image_input, decoder, aux_input])
This does not work, because it protests that the layer is dynamic and needs to be built with dynamic=True. When I try that, I get a RecursionError as follows:
 RecursionError Traceback (most recent call last) <ipythoninput15a40adb50e97d> in <module> 7 return [im2,im1] 8 aux_input = Input(shape=(1)) > 9 paired_images = Lambda(image_scrambler,dynamic=True)([image_input, decoder, aux_input]) c:\users\vilhelm\appdata\local\programs\python\python36\lib\sitepackages\tensorflow_core\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs) 791 # TODO(fchollet): consider py_func as an alternative, which 792 # would enable us to run the underlying graph if needed. > 793 outputs = self._symbolic_call(inputs) 794 795 if outputs is None: c:\users\vilhelm\appdata\local\programs\python\python36\lib\sitepackages\tensorflow_core\python\keras\engine\base_layer.py in _symbolic_call(self, inputs) 2126 def _symbolic_call(self, inputs): 2127 input_shapes = nest.map_structure(lambda x: x.shape, inputs) > 2128 output_shapes = self.compute_output_shape(input_shapes) 2129 2130 def _make_placeholder_like(shape): c:\users\vilhelm\appdata\local\programs\python\python36\lib\sitepackages\tensorflow_core\python\keras\utils\tf_utils.py in wrapper(instance, input_shape) 304 if input_shape is not None: 305 input_shape = convert_shapes(input_shape, to_tuples=True) > 306 output_shape = fn(instance, input_shape) 307 # Return shapes from `fn` as TensorShapes. 308 if output_shape is not None: c:\users\vilhelm\appdata\local\programs\python\python36\lib\sitepackages\tensorflow_core\python\keras\layers\core.py in compute_output_shape(self, input_shape) 808 with context.eager_mode(): 809 try: > 810 return super(Lambda, self).compute_output_shape(input_shape) 811 except NotImplementedError: 812 raise NotImplementedError( c:\users\vilhelm\appdata\local\programs\python\python36\lib\sitepackages\tensorflow_core\python\keras\engine\base_layer.py in compute_output_shape(self, input_shape) 552 try: 553 if self._expects_training_arg: > 554 outputs = self(inputs, training=False) 555 else: 556 outputs = self(inputs) ... last 5 frames repeated, from the frame below ... c:\users\vilhelm\appdata\local\programs\python\python36\lib\sitepackages\tensorflow_core\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs) 791 # TODO(fchollet): consider py_func as an alternative, which 792 # would enable us to run the underlying graph if needed. > 793 outputs = self._symbolic_call(inputs) 794 795 if outputs is None: RecursionError: maximum recursion depth exceeded while calling a Python object
So that doesn't actually tell me anything about why it didn't work, it just crashed.
I'd prefer not to have to fiddle around with building a layer class inhering from Layer if there's any way to get the less complicated method to work.

Diificulty Understanding Output of a For Loop
So I am new to coding (and StackOverflow) and practicing on CodeWars. I have wrote this for loop to count vowels, but when doing the sample test, I am receiving an output I do not understand.
def getCount(inputStr): num_vowels = 0 # your code here for i in inputStr: if i == 'a' or 'e' or 'i' or 'o' or 'u' or 'A' or 'E' or 'I' or 'O' or 'U': num_vowels += 1 print(num_vowels) return num_vowels
I get an output of 11 instead of 5 when running the test case "abracadabra". I wanted to understand the reasoning behind getting 11 instead of 5.
def getCount(inputStr): num_vowels = 0 # your code here for i in inputStr: if i == 'a': num_vowels += 1 print(num_vowels) return num_vowels
Running the code above would allow me to succeed in my sample test but obviously would fail in the test cases. Also is there any way to optimize my code because writing out every single vowel and adding or seems to be wrong. Thanks for the help!

Understanding the difference between numpy.log and numpy.sum for custom array containers
I am creating a custom container for a NumPy array by following the instructions on the SciPy website. I've written the following code which:
 creates a class,
NpContainer
 defines custom behavior for the functions
np.sum
andnp.log
to output a string.
import numpy as np HANDLED_FUNCTIONS = {} class NpContainer: def __init__(self, val): self.val = val def __array__(self): return np.array(self.val) def __array_function__(self, func, types, args, kwargs): if func not in HANDLED_FUNCTIONS: raise NotImplementedError() return HANDLED_FUNCTIONS[func](*args, **kwargs) def implements(np_function): def decorator(func): HANDLED_FUNCTIONS[np_function] = func return func return decorator @implements(np.sum) def sum(a, **kwargs): return 'Sum Val: {}'.format(np.sum(a.val, **kwargs)) @implements(np.log) def log(a, **kwargs): return 'Log Val: {}'.format(np.log(a, **kwargs))
I test the code using:
if __name__ == "__main__": container1 = NpContainer(val=np.array([1., 2.])) sum_result = np.sum(container1) print(sum_result) print(type(sum_result)) log_result = np.log(container1) print(log_result) print(type(log_result))
Sum produces the expected result.
Sum Val: 3.0 <class 'str'>
However
np.log
returns a NumPy array instead of a string.[0. 0.69314718] <class 'numpy.ndarray'>
Does anyone know why
np.log
skips my customdefined function? Any help is appreciated!  creates a class,