0

I'm trying to add new values from a previous df into a newly created df.

new_df = pd.DataFrame(columns=['org1_name','org1_desc','org2_name','org2_desc','score'])
new_df.iloc[0,0] = previous_df.name[0]

previous_df.name[0] gives me

9958    SETA BioMedicals
Name: name, dtype: object

and I just want "SETA BioMedicals".

However, previous_df.name[0] keeps giving me a pandas.core.series.Series instead of a string, and when I add to_string to it, it just changes the type to a method instead of a string. Any help would be appreciated!

Brian Guan
  • 193
  • 2
  • 12

2 Answers2

0

Your new dataframe is empty so you can not insert values into iloc[0,0]. Instead you need to append the dataframe as shown below. Also note that the append method does not mutate the dataframe, so you need to assign the returned value to a variable.

#I imagine this is what a portion of your previous_df looks like:
previous_df = pd.DataFrame({'name': ['SETA BioMedicals']})

new_df = pd.DataFrame(columns=['org1_name','org1_desc','org2_name','org2_desc','score'])
new_df = new_df.append({'org1_name': previous_df['name'][0]}, ignore_index=True)

print(new_df)
#output:
          org1_name org1_desc org2_name org2_desc score
0  SETA BioMedicals       NaN       NaN       NaN   NaN
pakpe
  • 5,391
  • 2
  • 8
  • 23
0

Use squeeze method if you want to convert your 1D axis into single value.

>>> pd.Series("SETA BioMedicals", index=[9958])
9958    SETA BioMedicals
dtype: object
>>> pd.Series("SETA BioMedicals", index=[9958]).squeeze()
'SETA BioMedicals'

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.squeeze.html

Corralien
  • 109,409
  • 8
  • 28
  • 52