You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PUBG游戏killed_by列死亡方式统计及直方图绘制报错排查

解决PUBG死亡方式统计与直方图绘制的语法错误问题

首先,你代码里的语法错误非常明显:df.(['...']).counter 完全不符合Python/Pandas的语法规范,这也是报错的直接原因。下面我们一步步实现你需要的统计与可视化功能:

步骤1:统计死亡方式的出现次数

Pandas提供了简洁的方法来统计列中各类值的频次,无需复杂的手动列名调用:

统计所有死亡方式的频次

如果你想查看killed_by列中所有死亡方式的出现次数,直接用value_counts()即可,它会自动统计所有唯一值的数量:

# 统计全部死亡方式的次数
death_counts = df['killed_by'].value_counts()
# 打印结果,按频次从高到低排序
print(death_counts)

仅统计指定的死亡方式

如果你只想统计你列出的那些特定死亡方式/武器,可以先筛选出目标值再统计:

# 定义需要统计的死亡方式列表
target_death_types = ['Grenade', 'SCAR-L', 'S686', 'Down and Out', 'M416', 'Punch', 'AKM', 'P92', 'Win94', 'M16A4', 'S12K', 'Hit by Car', 'death.WeapSawnoff_C', 'Kar98k', 'Tommy Gun', 'S1897', 'Bluezone', 'Micro UZI', 'P1911', 'SKS', 'Mini 14', 'Mk14', 'Falling', 'UMP9', 'P18C', 'Machete', 'Sickle', 'Groza', 'Crossbow', 'Drown', 'Vector', 'R1895', 'M249', 'Uaz', 'M24', 'VSS', 'Pan', 'AWM', 'RedZone', 'Motorbike', 'Buggy', 'death.ProjMolotov_DamageField_C', 'Dacia', 'DP-28', 'R45', 'Motorbike (SideCar)', 'death.Buff_FireDOT_C', 'Crowbar', 'AUG', 'Van', 'Pickup Truck', 'Aquarail', 'Boat', 'death.ProjMolotov_C', 'death.PG117_A_01_C', 'death.RedZoneBomb_C']

# 筛选出目标数据并统计频次
filtered_death_counts = df[df['killed_by'].isin(target_death_types)]['killed_by'].value_counts()
print(filtered_death_counts)

步骤2:绘制可视化图表(条形图更适合分类数据)

因为死亡方式是分类变量,用条形图比直方图更直观(直方图更适合连续数据),这里用Matplotlib实现:

import matplotlib.pyplot as plt

# 设置图形大小,避免标签重叠
plt.figure(figsize=(18, 8))

# 绘制条形图
filtered_death_counts.plot(kind='bar', color='steelblue')

# 添加图表标题与标签
plt.title('PUBG Death Method & Weapon Occurrences', fontsize=16)
plt.xlabel('Death Method / Weapon', fontsize=12)
plt.ylabel('Number of Deaths', fontsize=12)

# 旋转X轴标签,优化可读性
plt.xticks(rotation=45, ha='right')

# 自动调整布局,避免标签被截断
plt.tight_layout()

# 显示图表
plt.show()

如果一定要用直方图(虽然不推荐分类数据使用),可以这样写:

plt.figure(figsize=(18, 8))
plt.hist(df['killed_by'], bins=len(death_counts), color='coral')
plt.title('PUBG Death Method Histogram', fontsize=16)
plt.xlabel('Death Method / Weapon', fontsize=12)
plt.ylabel('Frequency', fontsize=12)
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
plt.show()

额外提示

  • 你写的df.killed_by.unique()是正确的,可以用来获取所有唯一的死亡方式,确认你的目标列表是否覆盖了所有需要统计的类型。
  • 确保你已经正确加载了数据集(比如通过pd.read_csv('your_pubg_data.csv')),否则df变量会不存在。

内容的提问来源于stack exchange,提问作者Rick J

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:10:01