Carol.Kar
Carol.Kar

Reputation: 5355

Adding new dataframe to one element

I am using pandas and want to create the following data frame:

| **Marketprice** | **m2** | **rent** |
|-----------------|--------|----------|
| 50000           | 25     | 500      |
| 50000           | 25     | 501      |
| 50000           | 25     | 502      |
| ...             | ...    | ...      |
| 50000           | 26     | 500      |
| 50000           | 26     | 501      |
| ...             | ...    | ...      |
| 51000           | 25     | 500      |
| 51000           | 25     | 501      |
| ...             | ...    | ...      |

So basically for each field in the marketprice column I want to have a field for sqm and rent.

I tried the following:

import numpy as np
import pandas as pd

marketPrice = np.arange(50000, 300000, 1000)
m2 = np.arange(25, 180, 1)
rent = np.arange(500, 900, 1)


df = pd.DataFrame(data=marketPrice, columns=["Marketprice"])
df1 = pd.DataFrame(data=m2, columns=["m2"])
df2  = pd.DataFrame(data=rent, columns=["rent"])

# add m2 to each marketprice
df.merge(df1,how='left', left_on=['Marketprice', "m2"], right_on=['Marketprice', "m2"])


df

However, I get the following error:

---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

<ipython-input-36-1ca75aa9724b> in <module>
     16 df2  = pd.DataFrame(data=m2, columns=["rent"])
     17 
---> 18 df.merge(df1,how='left', left_on=['Marketprice', "m2"], right_on=['Marketprice', "m2"])
     19 
     20 

4 frames

/usr/local/lib/python3.7/dist-packages/pandas/core/generic.py in _get_label_or_level_values(self, key, axis)
   1777             values = self.axes[axis].get_level_values(key)._values
   1778         else:
-> 1779             raise KeyError(key)
   1780 
   1781         # Check for duplicates

KeyError: 'Marketprice'

Any suggestions what I am doing wrong?

Upvotes: 0

Views: 44

Answers (1)

dermen
dermen

Reputation: 5362

You can use join:

df3 = df.join(df1, how='cross').join(df2, how='cross')
Out[1]: 
          Marketprice   m2  rent
0               50000   25   500
1               50000   25   501
2               50000   25   502
3               50000   25   503
4               50000   25   504
...               ...  ...   ...
15499995       299000  179   895
15499996       299000  179   896
15499997       299000  179   897
15499998       299000  179   898
15499999       299000  179   899

Upvotes: 1

Related Questions