Sometimes, we want to insert a list into a cell with Python Pandas.
In this article, we’ll look at how to insert a list into a cell with Python Pandas.
How to insert a list into a cell with Python Pandas?
To insert a list into a cell with Python Pandas, we can assign the list we want to insert to the location in the data frame we want to insert it in.
For instance, we write:
import pandas as pd
df = pd.DataFrame(data={'A': [1, 2, 3], 'B': ['x', 'y', 'z']})
df.at[1, 'B'] = ['c', 'd']
We create a data frame by using the DataFrame
class with a dictionary.
Then we assign the list ['c', 'd']
into the location [1, 'B']
with:
df.at[1, 'B'] = ['c', 'd']
Therefore, df
is now:
A B
0 1 x
1 2 [c, d]
2 3 z
Conclusion
To insert a list into a cell with Python Pandas, we can assign the list we want to insert to the location in the data frame we want to insert it in.