How Best to Insert Nan Values in a Python List by Referring to an Already Sorted List

How best to insert NaN values in a Python list by referring to an already sorted list

You could try something like this:

Sorted_List = [1,2,3,4]

Master_List =
[[1],
[1,2],
[1,4],
[3]]

Converted_Master_List = []
for part in Master_List:
new_part = [NaN] * len(Sorted_List)
for i in range(len(Sorted_List)):
if Sorted_List[i] in part:
new_part[i] = Sorted_List[i]
Converted_Master_List.append(new_part)

Can't append NaN to python list

In line 14 np=df1.to_numpy() you reassigned variable np from a package to a numpy array. So when you called np.nan it was searching nan from the numpy ndarray instance, not the package.

Change the variable to any other name and it will work fine.

How can i insert values from a list into a pandas data frame column?

You can use Numpy's searchsorted.

After you create a new_y array that is the same length as the new_x array. You use searchsorted to identify where in the new_y array you need to drop the old y values.

new_y = np.full(len(new_x), np.nan, np.float64)
new_y[np.searchsorted(new_x, df.x)] = df.y

pd.DataFrame({'x': new_x, 'y': new_y})

x y
0 0.00 3.0
1 0.03 NaN
2 0.07 4.0
3 0.10 6.0
4 0.13 5.0
5 0.17 NaN
6 0.20 NaN

numpy.insert() invalid slice -- Trying to Insert NaN in Numpy Array

Your code is currently attempting to insert 0 at index np.nan. Switch the args around:

a = np.insert(a, 0, np.nan)

Insert NaN if key is missing in dictionary

This task can be accomplished simply using the dict.get() function. Documentation here.

Example:

empty = {'a':'', 'b':'', 'c':'', 'd':''}
filled = {'a':'1', 'c':'2'}

result = {k: filled.get(k, 'NaN') for k in empty}

Output:

{'a': '1', 'b': 'NaN', 'c': '2', 'd': 'NaN'}

Notes:

If you'd prefer the 'proper' nan value, which is a float data type and can be used in None and isnull()-like validation, replace the 'NaN' with float('nan').

Inserting a value to list according to a threshold value

Here is the probable solution to the stated problem though the output is not matching with your desired outcome. But sharing on the basis of how I understood the problem.

a = [1, 2, 3, 6, 8, 12, 13, 18, 33, 23]
b = []
b.append(a[0])

threshold = 1 # Set Threshold value

for index in range(len(a)):
difference = 0
for i in range(len(b)):
difference = abs(a[index] - b[i])

if difference > threshold:
continue # Keep comparing other values in list b
else:
break # No need for further comparison

if difference > threshold:
b.append(a[index])



print("\nOutput list is")
print(b)

Output is:

Output list is
[1, 3, 6, 8, 12, 18, 33]

Also, I notice that after swapping the last two elements (33 <-> 23 ) of the list a as below:

a = [1, 2, 3, 6, 8, 12, 13, 18, 23, 33]

and running the same code. the output was near to your desired output:

Output list is
[1, 3, 6, 8, 12, 18, 23, 33]

This problem is very interesting now as I put myself into more investigation. And I found it a very interesting. Let me explain. First consider the list a as a list of integer numbers starting from 1 to N. For example:

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

and set the threshold to 1

threshold = 1   # Set Threshold value

Now, run the programme with threshold = 1 and you will get the output:

Output list is
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

and if you rerun with threshold = 2, you will get the following output:

threshold = 2

Output list is
[1, 4, 7, 10, 13, 16, 19]

Basically, this programme is basically generating a hopping series of integer numbers where hopping is set to the threshold value.

Interesting!!! Isn't it???

How to account for ties in returning top n values?

def top_items(item_counts, n=3):
counts = Counter(item_counts)
top_n_values = set([x[1] for x in c.most_common(n)])
output = {k:v for k,v in d.items() if v in top_n_values}
return sorted(output, key=output.get, reverse=True)

d = {'aberdeen': 5,
'amsterdam': 6,
'amsterdam?fussa': 6,
'anchorage': 12,
'andalucia?granada': 5,
'ann arbor': 6,
'aral': 6,
'arlington': 6,
'asia?london': 6,}

top_items(d)

Output

['anchorage',
'amsterdam',
'amsterdam?fussa',
'ann arbor',
'aral',
'arlington',
'asia?london']

Append list in list of lists

Since you have only one missing value at once, this should work:

lst=[[0, 0.05],
[1, 0.02],
[3, 0.02],
[5, 0.01]]

finlst = [lst[0]]
for ll in lst[1:]:
lind = finlst[-1][0]
if ll[0] - lind == 1:
finlst.append(ll)
else:
finlst.extend([[lind+1, ll[1]/2], [lind+2, ll[1]/2]])

finlst is: [[0, 0.05], [1, 0.02], [2, 0.01], [3, 0.01], [4, 0.005], [5, 0.005]].

And since we are here, I propose a more general solution working also in case where there is more than one missing value.

finlst = [lst[0]]
for ll in lst[1:]:
lastind = finlst[-1][0]
toadd = ll[0] - lastind
for i in range(toadd):
finlst.append([lastind+i+1, ll[1]/toadd])


Related Topics



Leave a reply



Submit