Related
I have a dataframe as below. I am trying to check if there is 0 or 1 in the vector column, if yes,
add 10 to the vector and divide by adding 2 to the vector otherwise keep the same vector.
df = pd.DataFrame({'user': ['user 1', 'user 2', 'user 3'],
'vector': [[0.01, 0.07, 0.0, 0.14, 0.0, 0.55, 0.11],
[0.12, 0.27, 0.1, 0.14, 0.1, 0.09, 0.19],
[0.58, 0.07, 0.02, 0.14, 0.04, 0.06, 1]]})
df
Output:
user vector
0 user 1 [0.01, 0.07, 0.0, 0.14, 0.0, 0.55, 0.11]
1 user 2 [0.12, 0.27, 0.1, 0.14, 0.1, 0.09, 0.19]
2 user 3 [0.58, 0.07, 0.02, 0.14, 0.04, 0.06, 1]
I used the following code:
df['vector']=df.apply(lambda x: x['vector']+10/(x['vector']+2) if x['vector']==0|1 else x['vector'], axis=1)
But the Output:
user vector
0 user 1 [0.01, 0.07, 0.0, 0.14, 0.0, 0.55, 0.11]
1 user 2 [0.12, 0.27, 0.1, 0.14, 0.1, 0.09, 0.19]
2 user 3 [0.58, 0.07, 0.02, 0.14, 0.04, 0.06, 1]
The expected output:
Use a list comprehension (faster than apply):
df['vector'] = [[x+10/(x+2) if x in [0,1] else x for x in v] for v in df['vector']]
Output:
user vector
0 user 1 [0.01, 0.07, 5.0, 0.14, 5.0, 0.55, 0.11]
1 user 2 [0.12, 0.27, 0.1, 0.14, 0.1, 0.09, 0.19]
2 user 3 [0.58, 0.07, 0.02, 0.14, 0.04, 0.06, 4.333333333333334]
This question already has answers here:
Cosinus of an array in Python
(3 answers)
Closed 1 year ago.
so this is the code:
def s(t):
return math.cos(2*(math.pi)*10*t)
s1_n = s(n1T)
error message: "only size-1 arrays can be converted to Python scalars"
this is the output for
n1T
"array([0. , 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ,
0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 , 0.21,
0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 , 0.31, 0.32,
0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 , 0.41, 0.42, 0.43,
0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 , 0.51, 0.52, 0.53, 0.54,
0.55, 0.56, 0.57, 0.58, 0.59, 0.6 , 0.61, 0.62, 0.63, 0.64, 0.65,
0.66, 0.67, 0.68, 0.69, 0.7 , 0.71, 0.72, 0.73, 0.74, 0.75, 0.76,
0.77, 0.78, 0.79, 0.8 , 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87,
0.88, 0.89, 0.9 , 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98,
0.99, 1. ])"
this are the used modules:
from thkdsp import * from dsplab import * from numpy import arange,
shape, array, zeros, size, ones, isscalar %pylab inline from
thkdsp.interpolation import ideal_interpolation from
thkdsp.interpolation import ideal_interpolation_mat
how to solve this?
-thanks!!
Found it myself :)
Just use numpy.cos(...) and it works perfectly.
I am trying to check for shared elements between two arrays. Here are the arrays and how they are formed:
# get range between two values from a df
d = pd.DataFrame([[0.06, 0.81]], columns=['start','stop'])
# rounding to 2 to enforce 2 sig digits
a = np.arange(
np.around(d.iloc[0]['start'], 2),
np.around(d.iloc[0]['stop'], 2),
.01
)
and the other array:
b = np.around(0.6999999999999993,2), np.around(1.2400000000000002,2)
b = np.arange(
b[0],
b[1],
.01
)
Now, I want to check if they share any values:
bool(set(a) & set(b))
This gives me False. It should be True. It should be true because this is what a and b look like printed out:
# a
array([0.06, 0.07, 0.08, 0.09, 0.1 , 0.11, 0.12, 0.13, 0.14, 0.15, 0.16,
0.17, 0.18, 0.19, 0.2 , 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27,
0.28, 0.29, 0.3 , 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38,
0.39, 0.4 , 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49,
0.5 , 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6 ,
0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 , 0.71,
0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ])
# b
array([0.7 , 0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ,
0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9 , 0.91,
0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1. , 1.01, 1.02,
1.03, 1.04, 1.05, 1.06, 1.07, 1.08, 1.09, 1.1 , 1.11, 1.12, 1.13,
1.14, 1.15, 1.16, 1.17, 1.18, 1.19, 1.2 , 1.21, 1.22, 1.23])
I get this weird behavior where if I print out a and b in my jupyter notebook, and then copy and paste the results into new variables and rerun the test, I get True. See below:
a = [0.06, 0.07, 0.08, 0.09, 0.1 , 0.11, 0.12, 0.13, 0.14, 0.15, 0.16,
0.17, 0.18, 0.19, 0.2 , 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27,
0.28, 0.29, 0.3 , 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38,
0.39, 0.4 , 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49,
0.5 , 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6 ,
0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 , 0.71,
0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ]
b = [0.7 , 0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ,
0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9 , 0.91,
0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1. , 1.01, 1.02,
1.03, 1.04, 1.05, 1.06, 1.07, 1.08, 1.09, 1.1 , 1.11, 1.12, 1.13,
1.14, 1.15, 1.16, 1.17, 1.18, 1.19, 1.2 , 1.21, 1.22, 1.23]
bool(set(a) & set(b))
This made me think that the arrays needed to be lists, so I ran this with the original a and b data:
bool(set(list(a)) & set(list(b)))
but still False.
Any ideas?
how I can create matrix using python numpy such this
array([[ 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ],
[ 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 ],
[ 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 ],
[ 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 ],
[ 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 ],
[ 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6 ],
[ 0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 ],
[ 0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ],
[ 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9 ],
[ 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1. ]])
Do this:
np.linspace(0.01, 1, 100).reshape(10, 10)
output:
array([[0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ],
[0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 ],
[0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 ],
[0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 ],
[0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 ],
[0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6 ],
[0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 ],
[0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ],
[0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9 ],
[0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1. ]])
import numpy as np
np.array([[(i+j)/100 for i in range(1, 11)] for j in range(0, 100, 10)])
I have following code
round_subset_list = subset.round(2).values.tolist()
print(round_subset_list)
The result is
[0.47, -0.36, -0.5, 0.2, 0.35, 1.82, -0.78, -0.91, 0.36, -1.74, 0.24, 0.76, 0.57, 2.32, 1.55, -1.31, -0.09, -0.02, -0.07, -0.19, -0.25, -1.09, 0.64, 1.22, -0.56, 1.76, 0.13, 1.33, -0.74, -1.15, 1.63, 1.04, -0.26, 0.02, -1.2, 0.37, 0.43, 0.04, 1.34, 0.57, 0.76, -1.25, -0.05, 0.12, 0.8, -0.99, -0.11, -0.54, -0.08, -0.04, -0.76, -0.8, 0.35, 1.54, -0.99, -0.35, -0.28, 0.45, -0.04, -0.06, 0.02, 0.58, -0.32, -0.1, 0.28, 0.3, -0.36, 0.81, 0.79, 0.21, 1.81, 0.19, 0.84, 0.2, -0.06, -0.11, -1.4, -2.08, 0.88, -0.14, -0.96, 1.3, 0.06, -0.37, 1.49, -0.91, 1.14, -1.05, 1.49, -0.79, 2.02, 0.38, 2.4, 1.25, 0.5, 1.11, -0.54, -0.1, 0.63, 1.01]
I wanna convert them into something look like ['0.47', '-0.36', '-0.5', '0.2', ...]
subset.string = ''.join(str(e) for e in round_subset_list)
print(subset.string)
The above code doesn't work
Try this.
round_subset_list = [str(i) for i in round_subset_list]
You can use the map fuction.
round_subset_list = [0.47, -0.36, -0.5, 0.2, 0.35, 1.82, -0.78, -0.91, 0.36, -1.74, 0.24, 0.76, 0.57, 2.32, 1.55, -1.31, -0.09, -0.02, -0.07, -0.19, -0.25, -1.09, 0.64, 1.22, -0.56, 1.76, 0.13, 1.33, -0.74, -1.15, 1.63, 1.04, -0.26, 0.02, -1.2, 0.37, 0.43, 0.04, 1.34, 0.57, 0.76, -1.25, -0.05, 0.12, 0.8, -0.99, -0.11, -0.54, -0.08, -0.04, -0.76, -0.8, 0.35, 1.54, -0.99, -0.35, -0.28, 0.45, -0.04, -0.06, 0.02, 0.58, -0.32, -0.1, 0.28, 0.3, -0.36, 0.81, 0.79, 0.21, 1.81, 0.19, 0.84, 0.2, -0.06, -0.11, -1.4, -2.08, 0.88, -0.14, -0.96, 1.3, 0.06, -0.37, 1.49, -0.91, 1.14, -1.05, 1.49, -0.79, 2.02, 0.38, 2.4, 1.25, 0.5, 1.11, -0.54, -0.1, 0.63, 1.01]
print(list(map(str, round_subset_list)))