簡體   English   中英

熊貓新列等於有條件的另一列

[英]pandas new column equals another column with condition

有一個名為data的pd數據庫:

                 transaction_id  house_id    date_sale  sale_price boolean_2015
0                     1         1  31 Mar 2016    £880,000         True
3                     4         2  31 Mar 2016    £450,000         True
4                     5         3  31 Mar 2016    £680,000         True
6                     7         4  31 Mar 2016  £1,850,000         True
7                     8         5  31 Mar 2016    £420,000         True

另一個叫房屋:

    id                                                  address  postcode       postcode first
0          1  Flat 78, Andrewes House, Barbican, London, Gre...  EC2Y 8AY       EC2Y  
1          2  Flat 35, John Trundle Court, Barbican, London,...  EC2Y 8DJ       EC2Y

問題是如何在名為“ postcode_first”的數據中添加一列,在其中查找data ['house_id']並將郵政編碼的第一部分添加到data ['postcode_first']的每一行中?
我最接近的是

data['postcode'] = np.where(houses['id'] == data['house_id'])

但這對所有幫助人員都沒有意義嗎? 編輯也嘗試過data['postcode'] = houses.loc[houses['id'] == data['house_id']]['postcode_first']

但這又回來了

    Traceback (most recent call last):
  File "/Users/saminahbab/Documents/House_Prices/final project/sql_functions.py", line 30, in <module>
    data['postcode'] = houses.loc[houses['id'] == data['house_id']]['postcode_first']
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/ops.py", line 735, in wrapper
    raise ValueError('Series lengths must match to compare')
ValueError: Series lengths must match to compare

這無關緊要,因為我實際上是要說data['postcode'] equals houses['postcode_first'] WHERE houses['id'] equals data['house_id']

您可以使用map()方法:

In [108]: df['postcode'] = df.house_id.map(h.set_index('id')['postcode first'])

In [109]: df
Out[109]:
   transaction_id  house_id    date_sale  sale_price boolean_2015 postcode
0               1         1  31 Mar 2016    £880,000         True     EC2Y
3               4         2  31 Mar 2016    £450,000         True     EC2Y
4               5         3  31 Mar 2016    £680,000         True      NaN
6               7         4  31 Mar 2016  £1,850,000         True      NaN
7               8         5  31 Mar 2016    £420,000         True      NaN

數據:

In [110]: h
Out[110]:
   id                                         address  postcode postcode first
0   1  Flat 78, Andrewes House, Barbican, London, Gre  EC2Y 8AY           EC2Y
1   2   Flat 35, John Trundle Court, Barbican, London  EC2Y 8DJ           EC2Y

In [113]: df
Out[113]:
   transaction_id  house_id    date_sale  sale_price boolean_2015
0               1         1  31 Mar 2016    £880,000         True
3               4         2  31 Mar 2016    £450,000         True
4               5         3  31 Mar 2016    £680,000         True
6               7         4  31 Mar 2016  £1,850,000         True
7               8         5  31 Mar 2016    £420,000         True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM