Convert integers in a dataframe column to a list

Blank

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.

jpp

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.

edited at
0

Comments

0 comments
Login to comment

Related

Convert Column of List to Dataframe

convert dataframe column which contains integers to dates

Convert array of strings in arrays of integers in a dataframe column

Convert Column of List to a Dataframe Column

Convert List to Pandas Dataframe Column

Convert list to a dataframe column in pyspark

Convert Column value in Dataframe to list

Convert dataframe column values into a list

Pandas convert column where every cell is list of strings to list of integers

convert pandas dataframe to list of tuple with unique pairs of integers as the first entry

Turn dataframe column into list of integers / floats (not string). tolist() not working

How to convert list into dataframe with specific column in python?

Convert spark DataFrame column to python list

Convert One Column in Python Dataframe to List

Convert PySpark dataframe column from list to string

convert a dataframe column from string to List of numbers

Convert list to a 1-column panda dataframe

convert dataframe column of type 'object' into type list

Convert 1 column for each dataframe in list

how to convert a pandas column containing list into dataframe

Convert the each column of the dataframe in a list in to as.POSIXct

python dataframe how to convert set column to list

Convert PySpark DataFrame column with list in StringType to ArrayType

Convert a DataFrame with a list of JSONs column into a JSON

Convert lists in a dataframe column into a single list in R

Convert a list of integers to string

Convert a number to a list of integers

Convert list of strings into list of integers

convert a list of strings list into integers