0

I am fairly new to python and pandas and I am trying to do the following:

Here is my dataset:

df5
Out[52]: 
      NAME
0  JIMMcdonald
1   TomDickson
2    SamHarper

I am trying to extract the first three characters using lambda apply

Here is what I have tried:

df5["FirstName"] = df5.apply(lambda x: x[0:3],axis=1)

here is the result:

df5
Out[54]: 
          NAME    FirstName
0  JIMMcdonald  JIMMcdonald
1   TomDickson   TomDickson
2    SamHarper    SamHarper

I dont understand why it didnt work.. can someone help me?

Thank you

Ali Parahoo
  • 161
  • 2
  • 11
  • 1
    You don't need apply for easy operations, use: `df['NAME'].str[:3]` – Erfan Sep 13 '19 at 21:43
  • Possible duplicate of [Get first letter of a string from column](https://stackoverflow.com/questions/35552874/get-first-letter-of-a-string-from-column) – Erfan Sep 13 '19 at 21:44

1 Answers1

1

This is due to the difference between DataFrame.apply (which is what you're using) and Series.apply (which is what you want to use). The easiest way to fix this is to select the series you want from your dataframe, and use .apply on that:

df5["FirstName"] = df5["NAME"].apply(lambda x: x[0:3],axis=1)

Your current code is running the apply function once on each column, in which case it's selecting the first three rows. This fixed code is running the function on each value in the selected column.

Better, yet, as @Erfan pointed out in his comment, doing simple one-liner string operations like this can often be simplified using panda's .str, which allows you to operate on entire series of strings in much the same way you'd operate on a single string:

df5["FirstName"] = df5["NAME"].str[:3]
scnerd
  • 5,836
  • 2
  • 21
  • 36