I have a question. Sorry if it's very simple, I'm new to this and have struggled for several hours to do this without success.
a1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I am trying to divide the first element of a1 by the second element of a2, the second element of a1 by the third element of a2, the third element of a1 by the fourth element of a2, etc...it's a long list but this is a short form.
The new array or list should be something like this:
a3 = [(1/2, 2/3, 3/4, 4/5, 5/6, 6/7, 7/8, 8/9, 9/10]
Here is my code:
a1_new = a1[:-1]
a2_new = a1[1:]
a3 = a1_new/a2_new
return a3
The answer is not correct.
What is a better way to do this?
In Excel 365
={1,2,3,4,5,6,7,8,9,10}/{2,3,4,5,6,7,8,9,10,11}
Related
Would really appreciate some help with my Excel query.
If I have the following rows of values (always 7 values per row) of data in Excel (3 examples below) where data is coded as 1 or 2, does anyone know an Excel formula which can; detect WHEN in the row, the FIRST 1 appears?
For example;
2, 1, 2, 2, 2, 1, 1. (1 first appears at point 2)
2, 2, 2, 1, 2, 1, 1. (1 first appears at point 4)
2, 2, 1, 2, 2, 2, 2. (1 first appears at point 3)
Any help is appreciated!
Use the MATCH function:
=MATCH(1,A1:G1,0)
If the type of a column in dataframe is int, float or string, we can get its unique values with columnName.unique().
But what if this column is a list, e.g. [1, 2, 3].
How could I get the unique of this column?
I think you can convert values to tuples and then unique works nice:
df = pd.DataFrame({'col':[[1,1,2],[2,1,3,3],[1,1,2],[1,1,2]]})
print (df)
col
0 [1, 1, 2]
1 [2, 1, 3, 3]
2 [1, 1, 2]
3 [1, 1, 2]
print (df['col'].apply(tuple).unique())
[(1, 1, 2) (2, 1, 3, 3)]
L = [list(x) for x in df['col'].apply(tuple).unique()]
print (L)
[[1, 1, 2], [2, 1, 3, 3]]
You cannot apply unique() on a non-hashable type such as list. You need to convert to a hashable type to do that.
A better solution using the latest version of pandas is to use duplicated() and you avoid iterating over the values to convert to list again.
df[~df.col.apply(tuple).duplicated()]
That would return as lists the unique values.
I have a numpy array like this
array([[0, 0, 1],
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
and an array with values
array([1, 2, 3, 4])
I would like to replace the ones in the first two-dimensional array with the corresponding values in the second array. Each row of the first array has exactly one 1, and there is only 1 replacement in the second array.
Result:
array([[0, 0, 1],
[2, 0, 0],
[0, 3, 0],
[0, 0, 4]])
I would like an elegant solution to achieve this, without loops and such.
Let's say a is the 2D data array and b the second 1D array.
An elegant solution would be -
a[a==1] = b
For performance, leveraging the fact that there's exactly one 1 per row, we could also use indexing -
a[np.arange(len(a)),a.argmax(1)] = b
Selectively assign per row
If we want to selectively mask and asign values per row, we could use one more level of masking. So, let's say we have the rows to be selected as -
select_rows = np.array([1,3])
Then, we could do -
rowmask = np.isin(np.arange(len(a)),select_rows)
So, for the replacement for the first approach would be -
a[(a==1) & rowmask[:,None]] = b[rowmask]
And for the second one -
a[np.arange(len(a))[rowmask],a.argmax(1)[rowmask]] = b[rowmask]
Hello I'm new to python so I'm sure this is a simple answer but I'm trying to find how to count the number of values in a certain key in a dictionary from an input.
I've tried something like:
fruits = {'apple' : 1, 2, 3 , 'banana' : 4, 5, 6}
search_fruit = input('Enter the fruit name:')
count = len(fruits[search_fruit])
print(f'{search_fruit} has {count} values that are {fruits[search_fruit]}')
the output should be for example:
apple has 3 values that are 1 2 3
but instead i'm getting:
apple has 5 values that are 1 2 3
Values should be either tuples (1, 2, 3), or lists [1, 2, 3]. The input function is not practical. An input variable works best. Hope this helps.!
input_key = "apple"
fruits = {'apple' : [1, 2, 3] , 'banana' : [4, 5, 6]}
length = len(fruits[input_key])
print(length)
I have an array for instance M = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and I want to get the top 3 smallest which are: [1, 2, 3]. How do I do this in excel VBA?
I want to get 3 smallest values for each row, i, of the array Min_NDate(i, j) in my actual code as shown below:
For i = 1 To Total_Rows_Help
For j = 1 To Total_Rows_Help
Min_NDate(i, j) = Worksheets("Help Worksheet").Cells(i, 2) - Worksheets("Help Worksheet").Cells(j, 2)
Next j
Next i
Just use the application's SMALL function.
dim m as variant, k as long
m = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
for k=1 to 3
debug.print application.small(m, k)
debug.print application.large(m, k)
next k
I have included a worksheet's LARGE functionality as I want to empirically demonstrate that I am not simply writing off the first three elements of the array (which is sorted in an ascending manner).