I have a pandas dataframe with a column of integer values but I can't seem to figure out how to put those values within the column into a list.
So for example,
NUMBERS ------> NUMBERS
1 [1]
2 [2]
3 [3]
4 [4]
5 [5]
6 NaN
7 [7]
8 NaN
Thank you.
This is one way.
df['NUMBERS'] = df['NUMBERS'].apply(lambda x: [x])
However, this is discouraged unless you have a very specific reason, since you lose the vectorisation benefits of pandas
.
To control behaviour when your value is np.nan
:
df = pd.DataFrame({'NUMBERS': list(range(1, 8))})
df['NUMBERS'] = df['NUMBERS'].apply(lambda x: [int(x)] if not pd.isnull(x) \
in (6, 8) else np.nan)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments