1

I have a Pandas DataFrame with a column of lists. I want create a new column that is comprised of these same lists, minus one specific element:

[In]:
key1 = 'site channel fiscal_week'.split()
key2 = 'site dude fiscal_week'.split()
key3 = 'site eng fiscal_week'.split()

keys = pd.DataFrame({'key': [1,2,3],
                     'dims': [key1,key2,key3]})

keys

[Out]:
                         dims  key
[site, channel, fiscal_week]    1
[site, dude, fiscal_week]       2
[site, eng, fiscal_week]        3

Here is my approach that failed:

keys['reduced_dims'] = keys['dims'].remove('fiscal_week')

I need to be able to remove a specific element, not pop() off the last element.

Desired output:

[Out]:
                        dims  key  reduced_dims
[site, channel, fiscal_week]    1  [site, channel]
[site, dude, fiscal_week]       2  [site, dude]
[site, eng, fiscal_week]        3  [site, eng]
0

2 Answers 2

3

keys['dims'] is a pd.Series, not list, and there's no list.remove() method. You should use pd.Series.apply() method, which applies some function to the values in each row.

keys['reduced_dims'] = keys['dims'].apply(
    lambda row: [val for val in row if val != 'fiscal_week']
)

keys['reduced_dims']

Out:
0    [site, channel]
1       [site, dude]
2        [site, eng]
Name: reduced_dims, dtype: object

And you can't use just list.remove() function instead of list comprehension,

lambda row: [val for val in row if val != 'fiscal_week']

because list.remove() returns None and you will get such series:

keys['reduced_dims'] = keys['dims'].apply(lambda x: x.remove('fiscal_week'))
keys['reduced_dims']

Out:
0    None
1    None
2    None
Name: reduced_dims, dtype: object
Sign up to request clarification or add additional context in comments.

2 Comments

What is interesting about the last because remove acts "inplace" if you don't re-assign back to keys['reduced_dims'], your column series is still mutated and this will achieve the desired results.
I hadn't thought of applying list comprehension, but it makes perfect sense. Thanks!
0

You could try

def rem_fw(lst):
    lst.remove('fiscal_week')
    return lst

keys['reduced_dims'] = keys['dims'].apply(rem_fw)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.