Pandas column creation methods

The name of the picture


Pandas column creation methods



There are many methods for creating new columns in Pandas (I may have missed some in my examples so please let me know if there are others and I will include here) and I wanted to figure out when is the best time to use each method. Obviously some methods are better in certain situations compared to others but I want to evaluate it from a holistic view looking at efficiency, readability, and usefulness.



I'm primarily concerned with the first three but included other ways simply to show it's possible with different approaches. Here's your sample dataframe:


dataframe


df = pd.DataFrame({'a':[1,2,3],'b':[4,5,6]})



Most commonly known way is to name a new column such as df['c'] and use apply:


df['c']


apply


df['c'] = df['a'].apply(lambda x: x * 2)
df
a b c
0 1 4 2
1 2 5 4
2 3 6 6



Using assign can accomplish the same thing:


assign


df = df.assign(c = lambda x: x['a'] * 2)
df
a b c
0 1 4 2
1 2 5 4
2 3 6 6



Updated via @roganjosh:


df['c'] = df['a'] * 2
df
a b c
0 1 4 2
1 2 5 4
2 3 6 6



Using map (definitely not as efficient as apply):


map


apply


df['c'] = df['a'].map(lambda x: x * 2)
df
a b c
0 1 4 2
1 2 5 4
2 3 6 6



Creating a new pd.series and then concat to bring it into the dataframe:


pd.series


concat


dataframe


c = pd.Series(df['a'] * 2).rename("c")
df = pd.concat([df,c], axis = 1)
df
a b c
0 1 4 2
1 2 5 4
2 3 6 6



Using join:


join


df.join(c)
a b c
0 1 4 2
1 2 5 4
2 3 6 6





I'd argue that these are not the most common ways. df['c'] = df['a'] * 2. Much more efficient than lambda because it will be vectorized.
– roganjosh
12 mins ago




df['c'] = df['a'] * 2


lambda





Don't use apply for vectorized operations, such as * 2. Just multiply by the series. df['c'] = 2*df.a is what you want. No need to complicate
– RafaelC
11 mins ago


apply


* 2


df['c'] = 2*df.a





@RafaelC I've updated the question with that method. Obviously this is a very simple example and there's an optimal way to do add a column here but I'm more interested in other cases where it might not be so obvious.
– W Stokvis
7 mins ago





Sometimes I like to use assign when doing on the fly analysis so, I can create a new copy of the dataframe and revert back to the previous copy for trouble shooting. If you just do df['B'] = df['B'] *2 you've modified the data frame inplace. Where if you used df1 = df.assign(b=df['b']*2), you now have a copy.
– Scott Boston
7 mins ago


assign





@WStokvis "definitely not as efficient as apply" apply is not efficient at all
– RafaelC
6 mins ago




2 Answers
2



A succinct way would be:


df['c'] = 2 * df['a']



No need to compute the new column elementwise.



Why are you using lambda function?
You can easily achieve the above-mentioned task easily by


df['c'] = 2 * df['a']



This will not increase the overhead.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived