df_b[df_b_new['小区名称'].isna()] = df_b_new[df_b_new['小区名称'].isna()] 将表b中建立表a的小区名称索引
时间: 2023-12-06 15:45:11 浏览: 32
可以使用如下代码将表b中建立表a的小区名称索引:
```
# 将表b中建立表a的小区名称作为索引
df_b_indexed = df_b.set_index('小区名称')
# 选出表a中小区名称为空的行
missing_names = df_a[df_a['小区名称'].isna()]
# 遍历缺失的行,将表b中对应小区名称的行复制到表a中
for index, row in missing_names.iterrows():
name = row['楼盘名称']
if name in df_b_indexed.index:
df_a.loc[index, '小区名称'] = name
df_a.loc[index, '所在区域'] = df_b_indexed.loc[name, '所在区域']
df_a.loc[index, '物业类型'] = df_b_indexed.loc[name, '物业类型']
```
这段代码将建立表a的小区名称索引,并遍历表a中小区名称缺失的行。对于每一行,它会在表b中查找同名的小区,如果找到了就将该行复制到表a中,同时将该小区的所在区域和物业类型也复制到表a中。
相关问题
if not df_used.empty: added = df_used.loc[df_used['pre_score'].isna(), :] df_used = df_used.loc[~df_used['pre_score'].isna(), :]
这段代码首先检查DataFrame对象`df_used`是否为空。如果不为空,它将执行两个操作:
第一个操作是使用`df_used['pre_score'].isna()`来选择出'pre_score'列为空的行,并将其赋值给变量`added`。这一步可以理解为将满足条件的行添加到`added`中。
第二个操作是使用`~df_used['pre_score'].isna()`来选择出'pre_score'列不为空的行,并将其重新赋值给`df_used`。这一步可以理解为更新`df_used`,将空值的行从中删除。
综合起来,这段代码的作用是将原始DataFrame中'pre_score'列为空的行从原始DataFrame中移除,并将这些行保存到变量`added`中。同时,更新了原始DataFrame,只保留了'pre_score'列不为空的行。
topic = df_course.loc[df_course['pid'].isna()]如何得到topic的index
要获取选定行的索引,可以使用 `pandas.DataFrame.index` 属性。因此,要获取 `topic` 的索引,可以使用以下代码:
```
topic_index = topic.index
```
其中,`topic` 是通过以下代码选择的 DataFrame:
```
topic = df_course.loc[df_course['pid'].isna()]
```
这将选择 `df_course` 中所有 `pid` 列为空值的行,并将其赋值给 `topic` 变量。然后,使用 `index` 属性获取 `topic` 的索引。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)