I have the following DF
ID FULL_NAME
1 John Dee
2 Mary Ann
I get this DF from a Postgres table using the following method:
cur = conn.cursor()
cur.execute(query)
headers = [i[0] for i in cur.description]
return cur.fetchall(), headers
Then I tranform the data in a dataframe using this code
df = pd.DataFrame(list(data))
df.columns = [header]
I want to generate a column in the DF named FIRST_NAME, I'm using this code to do this:
df['FIRST_NAME'] = df['FULL_NAME'].str.split().str[0].astype(str,errors='ignore')
df['FIRST_NAME'] = df['FIRST_NAME'].str.strip()
When I run this code I get the following error:
AttributeError: 'DataFrame' object has no attribute 'str'. Did you mean: 'std'?
If I remove the header part (df.columns = [header]
) I get another error:
KeyError: 'FULL_NAME'
When I get this data from SQL Server it generates correctly the FIRST_NAME
column. When I use Postgres I got this errors. Why is this happening?
CodePudding user response:
Given your dataframe's FULL_NAME
column, you can create a column of first names using:
# break out the first name
first_names = [nm.split()[0] for nm in df['FULL_NAME']]
and assign it as a new column in the dataframe:
df['FIRST_NAMES'] = first_names