I wish to have extract all the data for value column in form of array, which the output should be: [nan, 47, 47.9, 48.6]
Anyone can share me the ideas?
I wish to have extract all the data for value column in form of array, which the output should be: [nan, 47, 47.9, 48.6]
Anyone can share me the ideas?
Use values
for numpy array
+ numpy.ndarray.tolist
for list
:
L = df['value'].values.tolist()
#alternative
#L = df['value'].tolist()
Or convert to list
:
L = list(df['value'])
EDIT:
It seems you need convert to float
first by astype
:
df = pd.DataFrame({'value':['nan','47','47.9','48.6']})
print (df)
value
0 nan
1 47
2 47.9
3 48.6
L1 = df['value'].values.tolist()
print (L1)
['nan', '47', '47.9', '48.6']
L2 = df['value'].astype(float).values.tolist()
print (L2)
[nan, 47.0, 47.9, 48.6]
because if float
all values get different output - 47.0
not 47
:
df = pd.DataFrame({'value':[np.nan,47,47.9,48.6]})
value
0 NaN
1 47.0
2 47.9
3 48.6
L = df['value'].values.tolist()
print (L)
[nan, 47.0, 47.9, 48.6]
EDIT1:
If astype
return error e.g:
ValueError: could not convert string to float: 'a'
then need to_numeric
with parameter errors='coerce'
for convert non numeric to NaN
df = pd.DataFrame({'value':[np.nan,47,47.9,48.6, 'a']})
print (df)
value
0 NaN
1 47
2 47.9
3 48.6
4 a
print (pd.to_numeric(df['value'], errors='coerce'))
0 NaN
1 47.0
2 47.9
3 48.6
4 NaN
Name: value, dtype: float64
L = pd.to_numeric(df['value'], errors='coerce').values.tolist()
print (L)
[nan, 47.0, 47.9, 48.6, nan]