jakes
jakes

Reputation: 2085

Creating new columns with pandas .loc

My dataset looks like this:

ex = pd.DataFrame.from_dict({'grp1': np.random.choice('A B'.split(), 20), 'grp2': np.random.choice([1, 2], 20), 'var1': np.random.rand(20), 'var2': np.random.randint(20)})

I want to create new columns with the next value within the groups, but the following code results in SettingWithCopyWarning:

ex[['next_var1', 'next_var2']] = ex.groupby(['grp1', 'grp2'])[['var1', 'var2']].shift(-1)

Therefore I tried to use .loc:

ex.loc[:, ['next_var1', 'next_var2']] = ex.groupby(['grp1', 'grp2'])[['var1', 'var2']].shift(-1)

However, it results in error:

KeyError: "None of [Index(['next_var1', 'next_var2'], dtype='object')] are in the [columns]"

What's wrong with the .loc usage?

Upvotes: 0

Views: 92

Answers (1)

villoro
villoro

Reputation: 1549

With loc you can't create new columns. But you could do:

ex['next_var1'], ex['next_var2'] = None, None

ex.loc[:, ['next_var1', 'next_var2']] = ex.groupby(['grp1', 'grp2'])[['var1', 'var2']].shift(-1).values

However you could also do:

ex[['next_var1', 'next_var2']] = ex.groupby(['grp1', 'grp2'])[['var1', 'var2']].shift(-1)

Is what you tried but it works fine with python 3.7 and pandas 0.25.

Upvotes: 1

Related Questions