Get the first letter of a row from a column

I fight with pandas, and for now I lose. I have a source table like this:

import pandas as pd a=pd.Series([123,22,32,453,45,453,56]) b=pd.Series([234,4353,355,453,345,453,56]) df=pd.concat([a, b], axis=1) df.columns=['First', 'Second'] 

I would like to add a new column to this data frame with the first digit from the values ​​in the First column: a) change the number to a row from the First column b) extract the first character from the newly created row c) The results from b are saved as a new column in data frame

I do not know how to apply this to the pandas frame object. I would be grateful for helping me with this.

+21
python pandas
source share
2 answers

Move the dtype in col to str , and you can cut with the inscription str :

 In [29]: df['new_col'] = df['First'].astype(str).str[0] df Out[29]: First Second new_col 0 123 234 1 1 22 4353 2 2 32 355 3 3 453 453 4 4 45 345 4 5 453 453 4 6 56 56 5 

if you need, you can forward the dtype again by calling astype(int) in the column

+41
source share

.str.get

This is the easiest way to specify string methods.

 # Setup df = pd.DataFrame({'A': ['xyz', 'abc', 'foobar'], 'B': [123, 456, 789]}) df AB 0 xyz 123 1 abc 456 2 foobar 789 df.dtypes A object B int64 dtype: object 

For columns of a string (read: object ) type, use

 df['C'] = df['A'].str[0] # Similar to, df['C'] = df['A'].str.get(0) 

For non-numeric columns, you must first convert .astype , as shown in @Ed Chum's answer.

 df['D'] = df['B'].astype(str).str[0] 

 df ABCD 0 xyz 123 x 1 1 abc 456 a 4 2 foobar 789 f 7 

List comprehension and indexing

A simple list comprehension will work well and will probably be faster.

 # For string columns df['C'] = [x[0] for x in df['A']] # For numeric columns df['D'] = [str(x)[0] for x in df['B']] 

 df ABCD 0 xyz 123 x 1 1 abc 456 a 4 2 foobar 789 f 7 
0
source share

All Articles