选择特定列时的NaN值

时间:2019-09-20 11:09:42

标签: python pandas nan

当前尝试通过Pandas和Matplotlib收集有关数据科学的一些知识。

我想做什么
选择具有相应价格的邻居组以租用私人房间(AirBnB)

问题
我为一列获得了正确的数据,但对于其余列,我收到了NaN值。感谢任何帮助,您可以在下面找到代码段和输出。

    ap_df = pd.DataFrame()

for neighbourhood_group in df['neighbourhood_group'].unique():
    nbhg_df = df.copy()[df['neighbourhood_group']==neighbourhood_group]
    nbhg_df[f"{neighbourhood_group}_Price"] = nbhg_df['price']
    nbhg_df.set_index("id", inplace=True)
    nbhg_df.sort_index(inplace=True)
    if ap_df.empty:
        ap_df = nbhg_df[[f"{neighbourhood_group}_Price"]]
    else:
        ap_df = ap_df.join(nbhg_df[f"{neighbourhood_group}_Price"])

ap_df

Output I receive after performing the above Code

1 个答案:

答案 0 :(得分:0)

所以我想我可以通过另一个更简单,更适合您需求的示例来帮助您

import pandas

df = pandas.DataFrame({
    'id': [1, 2, 3],
    'cat': ['cat_1', 'cat_2', 'cat_3'],
    'price': [1, 2, 3]
})

df.set_index(df['id'], drop=True, inplace=True)
new_df = pandas.DataFrame(index=df.index)

for var in df['cat'].unique():
    new_df[f'{var}_price'] = df[df['cat'] == var]['price']


new_df

输出:

    cat_1_price   cat_2_price   cat_3_price
id          
1    1.0             NaN             NaN
2    NaN             2.0             NaN
3    NaN             NaN             3.0