Home > database >  Insert new column into dataframe as a VARIABLE
Insert new column into dataframe as a VARIABLE

Time:06-29

I used the following to obtain state using zipcode

from uszipcode import SearchEngine
engine = SearchEngine()
zipcode = engine.by_zipcode(ZIPCODE)

**st** = print(zipcode.state)

Now I am trying to insert state into data frame df2:

  • I can do it manually such as:
  df2.assign(STATE = 'Florida')

*Column name is STATE

  • However, new data will used with this scrip so I need to use a variable. How can I do that?

    This is my unsuccessful shot:

df2.assign(STATE = **st**)

CodePudding user response:

Show your code please but it seems that you only have to add it to the dataframe.

zipcode = zipcode.state  # Florida in this case
df2['STATE']= zipcode

CodePudding user response:

df['new_column'] = 'some value'

You can do the following:

pd.concat([df1, df2], axis=1)

where df1 is your dataframe and df2 is your new column.

  • Related