pandas统计数据里作者列[]或【】里的国家名出现次数,没有[]或【】则表示是中国作者
时间: 2023-09-22 18:06:37 浏览: 39
可以使用 Pandas 的 `str.contains()` 方法来查找包含指定字符串的行,然后再统计符合条件的行数即可。
假设你的 DataFrame 名称为 `df`,作者列的名称为 `author`,则可以按照以下步骤进行操作:
1. 先找出包含国家名的行:
``` python
df['country'] = df['author'].str.extract('\[(.*?)\]', expand=False) # 提取[]中的内容到新列
mask = df['country'].str.contains('国家名') # 创建包含国家名的行的布尔索引
```
2. 统计包含国家名的行数:
``` python
count = mask.sum() # 统计布尔索引中为 True 的个数
```
3. 统计没有 [] 或 【】 的中国作者数:
``` python
china_count = (df['country'].isna() & (df['author'].str.contains('中国') | df['author'].str.contains('CN'))).sum()
```
其中,`isna()` 方法用于判断该行的 `country` 列是否为空值,`&` 表示逻辑与操作,`|` 表示逻辑或操作。
最终的代码可能类似于:
``` python
df['country'] = df['author'].str.extract('\[(.*?)\]', expand=False)
mask = df['country'].str.contains('国家名')
count = mask.sum()
china_count = (df['country'].isna() & (df['author'].str.contains('中国') | df['author'].str.contains('CN'))).sum()
```
需要将 `国家名` 替换为你需要统计的国家名,同时将 `中国` 替换为你需要统计的其他国家名(如果有的话)。