I am trying to create a new column in a dataframe consisting of the first N elements from an existing list column. For example, given the dataframe:
import pandas as pd
df = pd.DataFrame({'words':[['red','blue','green'],
['blue','sky','cloud'],
['eat','food','nomnom']]
I would like to create a new column, words2
, that contains the first two words in each list. I tried df['words2'] = df['words'].str[:2]
, but it on returned the first two characters, not the first two elements.