lserlohn
lserlohn

Reputation: 6206

output from summing two dataframes

I have two dataframe, df1 (1 row, 10 columns) and df2 ( 7 rows, 10 columns). Now I want to add values from those two dataframe:

final = df1[0] + df2[0][0]

print final

output:
0    23.8458
Name: 0, dtype: float64

I believe 23.8458 is what I want, but I don't understand what "0" stand for, and what datatype of "final", but I just want to keep 23.8458 as a float number. How can I do that? Thanks,

Upvotes: 0

Views: 44

Answers (1)

EdChum
EdChum

Reputation: 393933

What you are printing is the Series where it has a single value with index value 0 and column value 23.8458 if you wanted just the value then print final[0] would give you what you want

You can see the type if you do print type(final)

Example:

In [42]:

df1 = pd.DataFrame(np.random.randn(1,7))
print(df1)
df2 = pd.DataFrame(np.random.randn(7,10))
print(df2)
          0         1         2         3         4         5         6
0 -1.575662  0.725688 -0.442072  0.622824  0.345227 -0.062732  0.197771
          0         1         2         3         4         5         6  \
0 -0.658687  0.404817 -0.786715 -0.923070  0.479590  0.598325 -0.495963   
1  1.482873 -0.240854 -0.987909  0.085952 -0.054789 -0.576887 -0.940805   
2  1.173126  0.190489 -0.809694 -1.867470  0.500751 -0.663346 -1.777718   
3 -0.111570 -0.606001 -0.755202 -0.201915  0.933065 -0.833538  2.526979   
4  1.537778 -0.739090 -1.813050  0.601448  0.296994 -0.966876  0.459992   
5 -0.936997 -0.494562  0.365359 -1.351915 -0.794753  1.552997 -0.684342   
6  0.128406  0.016412  0.461390 -2.411903  3.154070 -0.584126  0.136874   

          7         8         9  
0 -0.483917 -0.268557  1.386847  
1  0.379854  0.205791 -0.527887  
2 -0.307892 -0.915033  0.017231  
3 -0.672195  0.110869  1.779655  
4  0.241685  1.899335 -0.334976  
5 -0.510972 -0.733894  0.615160  
6  2.094503 -0.184050  1.328208  
In [43]:

final = df1[0] + df2[0][0]
print(final)
0   -2.234349
Name: 0, dtype: float64
In [44]:

print(type(final))
<class 'pandas.core.series.Series'>

In [45]:

final[0]
Out[45]:
-2.2343491912631328

Upvotes: 1

Related Questions