Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - GroupBy values based on first letter in the column

I have a column in a dataframe containing a sample of the following values:

     Cabin
0    A12
1    C27
2    D56
3    E87
4    G21
5    J64

I would like to create another column containing ONLY the first letter of that column:

     Cabin   New_Cabin
0    A12     A
1    C27     C
2    D56     D
3    E87     E
4    G21     G
5    J64     J
like image 543
Jeru Luke Avatar asked Jan 29 '23 18:01

Jeru Luke


1 Answers

Here it is, young Pandas apprentice.

df['my_easy_column'] = df.mycol.str[0]

like image 181
ℕʘʘḆḽḘ Avatar answered Feb 01 '23 07:02

ℕʘʘḆḽḘ