1

I'm doing some work where I'm referencing a single column of a dataframe and the doing other operations with it (e.g., merging).

When I select a dataframe column I find that I constantly have to change it back to a dataframe to merge or perform some other type of operation.

Is there a way to keep a single column a dataframe?

Example

df1

      astigmatism    nearsighted    glasses
Roy      2.0            yes         monofocal
Mary     0.0            no          plain
Peter    1.0            yes         bifocal    

and

df2

       height     weight     haircolor

Roy     72         159        brown
Mary    60         110        brown
Peter   65         130        black

I want to merge df1['glasses'] on to df2:

df3 = df2.merge(df1['glasses'], how='left', left_index=True, right_index=True)

this gives me an error:

 {ValueError}can not merge DataFrame with instance of type <class 'pandas.core.series.Series'>

but if I do this:

 df3 = df2.merge(pandas.DataFrame(df1['glasses']), how='left', left_index=True, right_index=True)

it works and I get desired result:

df3

       height     weight     haircolor  glasses

Roy     72         159        brown     monofocal
Mary    60         110        brown     plain
Peter   65         130        black     bifocal

I'm wondering if there is a systematic way to avoid having to avoid having to redefine the series as a dataframe and to just have that series be a one column dataframe.

Thanks in advance.

Windstorm1981
  • 2,564
  • 7
  • 29
  • 57

0 Answers0