python - 'DataFrame' 对象没有属性 'startswith' 错误
问题描述
我正在尝试使用startswith
对数据文件的某些部分进行排序,以便可以按名称在 matplotlib 上绘制它们。我用:
mint = open('/Users/brand/Desktop/datafile.txt')
str = ps.DataFrame(mint)
a = str.startswith('MS')
plt.scatter(color[a], light[a], c = 'blue')
b = str.startswith('BH')
plt.scatter(color[b], light[b], c = 'red')
c = str.startswith('RH')
plt.scatter(color[c], light[c], c = 'green')
d = str.startswith('RGB')
plt.scatter(color[d], light[d], c = 'purple')
e = str.startswith('AGB')
plt.scatter(color[e], light[e], c = 'pink')
但是每次我使用不同的模块运行它来打开文件时,它总是有错误:' ' object has no attribute 'startswith'
.
那么什么对象有startswith
我可以使用的属性呢?
解决方案
将您的数据框重命名为df_str
因为str
是python中的关键字。并df_str.str.startswith()
应用您的字符串方法
mint = open('/Users/brand/Desktop/datafile.txt')
df_str = ps.DataFrame(mint)[0]
a = df_str.str.startswith('MS')
plt.scatter(color[a], light[a], c = 'blue')
b = df_str.str.startswith('BH')
plt.scatter(color[b], light[b], c = 'red')
c = df_str.str.startswith('RH')
plt.scatter(color[c], light[c], c = 'green')
d = df_str.str.startswith('RGB')
plt.scatter(color[d], light[d], c = 'purple')
e = df_str.str.startswith('AGB')
plt.scatter(color[e], light[e], c = 'pink')
编辑:更改df_str = ps.DataFrame(mint)
为df_str = ps.DataFrame(mint)[0]
将 Dataframe 转换为 Series
推荐阅读
- java - 来自 macOS 10.13.6 上的 JNI 应用程序的“非法指令:4”
- android - 启用暗模式时 MODE_NIGHT_YES 和 MODE_NIGHT_FOLLOW_SYSTEM 之间的差异
- python - 验证嵌套字典的值类型
- sharepoint-2019 - 在 sharepoint 2019 online 中将顶部链接导航移至下方
- border - 如何制作透明边框
- charts - 如何使用 Google Visualization API 创建具有双 y 轴的图表?
- angular - 从 NbAuthOAuth2Token 角度获取用户数据
- android - 使用常规搜索 Google api 和 Retrofit
- javascript - useEffect 中的函数运行一次,而我想同时运行
- json - Wordpress REST API _fields 和 _embed 参数不能同时工作