有多个行等于first_card,为什么只显示了一个
时间: 2024-03-18 19:40:18 浏览: 26
这是因为在代码中,每当找到一个符合条件的行时,都会将该行的数据添加到 `level_cards` DataFrame 中,并将 `level_cards` DataFrame 与一个新的 DataFrame 进行拼接(concat),从而创建一个新的 DataFrame,并将该新的 DataFrame 赋值给 `level_cards`。由于每次拼接都会生成一个新的 DataFrame,因此最终 `level_cards` 中只会包含最后一个符合条件的行的数据。
如果希望 `level_cards` 中包含所有符合条件的行的数据,可以将代码修改为将每个符合条件的行的数据都添加到一个列表中,然后将该列表转换为 DataFrame,如下所示:
```
import pandas as pd
pd.set_option('display.unicode.east_asian_width', True)
df = pd.read_excel('G:\A5204990500002023050006\一级卡6228480269035098277.xlsx', index_col=0)
first_card = '6212250200006032450'
level_cards_data = []
for index, row in df.iterrows():
if row[2] == first_card:
level_cards_data.append(['一级卡', row[2]])
level_cards = pd.DataFrame(level_cards_data, columns=['卡级', '账号'])
print(level_cards)
```
这样,最终的 `level_cards` DataFrame 就会包含所有符合条件的行的数据。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)