选择特定列时的 NaN 值
NaN Values when selecting specific columns
目前正在尝试使用 Pandas 和 Matplotlib 收集一些有关数据科学的知识。
我想做什么
Select 租用私人房间 (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
所以我想我可以帮助您提供另一个更简单且可以满足您需求的示例
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
目前正在尝试使用 Pandas 和 Matplotlib 收集一些有关数据科学的知识。
我想做什么
Select 租用私人房间 (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
所以我想我可以帮助您提供另一个更简单且可以满足您需求的示例
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