I have a data frame that looks like this
dataframe
name address
Wings 3109 N Main St, Hope Mills, NC 28348
Burger King 407 N Eastern Blvd, Fayetteville, NC 28301
I am trying to convert the column address into a list to be able to get the fist element of each row: 3109 N Main St and 407 N Eastern Blvd
So far this is my code
df = pd.read_csv("mydataframe.csv")
df['address'] = list(df['address'])
df.loc[:, 'new_col'] = df.address.map(lambda x: x[0])
With this code I am getting the new_colum but I am getting the first character of the column address
name address new_col
Wings 3109 N Main St, Hope Mills, NC 28348 3
Burger King 407 N Eastern Blvd, Fayetteville, NC 28301 4
Expected Output
name address new_col
Wings 3109 N Main St, Hope Mills, NC 28348 3109 N Main St
Burger King 407 N Eastern Blvd, Fayetteville, NC 28301 407 N Eastern Blvd
CodePudding user response:
df['new_col'] = df['address'].str.split(',',1).str[0]
df
name address new_col
0 Wings 3109 N Main St, Hope Mills, NC 28348 3109 N Main St
1 Burger King 407 N Eastern Blvd, Fayetteville, NC 28301 407 N Eastern Blvd