0

I have this dataframe: enter image description here

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?

Shi Jie Tio
  • 2,479
  • 5
  • 24
  • 41
  • Possible duplicate of [get list from pandas dataframe column](http://stackoverflow.com/questions/22341271/get-list-from-pandas-dataframe-column) – McGrady May 09 '17 at 05:11

1 Answers1

2

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]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252