JPdL
JPdL

Reputation: 149

Get first character of each element of a column in a dataframe

I have a dataframe called df. It has a column called 'Spec Type'. Using pandas,

df['Spec Type']

0         NaN
1          A1
2         G7V
3       F7+K4
.         .
.         .
169       A0e

I want to get only the first character for each entry and make it as a new column of df called 'Spec Type Index'. However, the following code gives me an error:

df['Spec Type Index'] = [i[0] for i in df['Spec Type']]

'float' object is not subscriptable

The error is in the i[0] part. I use it to get the first character of the indexed element. What should I do?

Upvotes: 4

Views: 10280

Answers (1)

EdChum
EdChum

Reputation: 393963

df.loc[df['Spec Type'].notnull(), 'Spec Type Index'] = df['Spec Type'].str[0]

should work, the problem is the NaN is a float dtype so you can't use str methods, masking like the above avoids that

In [48]:
df.loc[df['Spec Type'].notnull(), 'Spec Type Index'] = df['Spec Type'].str[0]
df

Out[48]:
      Spec Type Spec Type Index
index                          
0           NaN             NaN
1            A1               A
2           G7V               G
3         F7+K4               F

Upvotes: 4

Related Questions