簡體   English   中英

從 Dataframe 中提取多列並為不存在的列返回 NaN

[英]Extract Multiple Columns from Dataframe and Return NaN for Columns that do not Exist

我正在嘗試從數據框中提取多列,如下所示。 我想通過調用它們的名稱來識別所需的列,並為數據框中不存在的列返回 NaN。

data_1 = {'host_identity_verified':['t','t','t','t','t','t','t','t','t','t'],
      'neighbourhood':['q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q'],

      'neighbourhood_cleansed':['Oostelijk Havengebied - Indische Buurt', 'Centrum-Oost', 'Centrum-West', 'Centrum-West', 'Centrum-West',
                                'Oostelijk Havengebied - Indische Buurt', 'Centrum-Oost', 'Centrum-West', 'Centrum-West', 'Centrum-West'],
     'neighbourhood_group_cleansed': ['NaN','NaN','NaN','NaN','NaN','NaN','NaN','NaN','NaN','NaN'],
      'latitude':[ 52.36575, 52.36509, 52.37297, 52.38761, 52.36719, 52.36575, 52.36509, 52.37297, 52.38761, 52.36719]}

df_1 = pd.DataFrame(data_1)

我知道這種獲取一列的方法:

x = df_1.get('neighbourhood_cleansed', pd.Series(index=df_1.index, name='neighbourhood_cleansed', dtype='object'))

但是我一次只能使用這種方法獲得一列。

我想做類似的事情:

columns_needed = [['host_identity_verified', 'neighbourhood', 'latitude', 'longitude', 'price']]

# x= some code to get me the columns above and return NaN for columns such as 'longitude' and 'price.

使用reindex function 將創建naan列並提取您需要的列:

df_1.reindex(['host_identity_verified', 'neighbourhood', 'latitude', 'longitude', 'price'], axis=1)

暫無
暫無

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

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