I have column with following values:
d = {'id': [1, 2, 3, 4, 5],
'value': [['Red', 'Blue', 'Yellow'],
['Blue', 'Yellow', 'Orange'],
['Green', 'Purple', 'Yellow', 'Red'],
['Violet', 'Blue', 'Green', 'Red', 'Brown'],
['Blue', 'Green']]}
df = pd.DataFrame(data = d)
And I want to break down column values, tuples of strings, into pairs to form a new column or list like that
d = {'value': [['Red', 'Blue'],
['Blue', 'Yellow'],
['Blue', 'Yellow'],
['Yellow', 'Orange'],
['Green', 'Purple'],
['Purple', 'Yellow'],
['Yellow', 'Red'],
['Violet', 'Blue'],
['Blue', 'Green'],
['Green', 'Red'],
['Red', 'Brown'],
['Blue', 'Green']]}
df = pd.DataFrame(data = d)
I do the breaking with apply(lambda x:)
function, however it returns only one pair of values.
def splitter(row):
for first, second in zip(row, row[1:]):
return [first, second]
pairs_list = df_gr.status.apply(lambda x: splitter(x))
I know that it can be with iterrows()
loop but I'd like to know a more efficient method.