python - Pandas 'Str' 对象没有属性 'to_csv'
问题描述
目前,我想知道为什么 Pandas 在返回时无法将数据帧转换为 csv 文件AttributeError: 'str' object has no attribute 'to_csv'
我一直在尝试trends.to_string(index=False).to_csv('test.csv'))
等以及其他一些示例,但它一遍又一遍地返回相同的内容。
def main(url):
google = GoogleAnalysis(url)
codes = country_codes()
return pd.concat([
google.trends(country_code)
for country_code in codes[:len(codes) // 2]
])
def trends(self,country_code):
df = pd.DataFrame(
self._retrieve_trends(country_code),
columns=['Title', 'Search_Score'],
)
df['Country Code'] = country_code
df['Platform'] = 'Google'
return df
if __name__ == '__main__':
trends = main('https://trends.google.com/trends/trendingsearches/daily/rss')
trends.to_csv('k.csv').to_string(index=False)
数据帧的输出
Title Search_Score Country Code Platform
アジアカップ 2019 20000 JP Google
康華 2000 HK Google
스피릿위시 2000 KR Google
Michelle Obama 50000 US Google
更新(包括
main
)
解决方案
您可能想要,下面的代码,您必须输入trends
方法的参数:
def trends(self,country_code):
df = pd.DataFrame(
self._retrieve_trends(country_code),
columns=['Title', 'Search_Score'],
)
df['Country Code'] = country_code
df['Platform'] = 'Google'
return df
if __name__ == '__main__':
trends = main(
'https://trends.google.com/trends/trendingsearches/daily/rss')
trends.to_csv('k.csv')
推荐阅读
- jquery - JQuery Inputmask 不显示分隔符
- php - PHP CURL 给我 401 错误,但在 POSTMAN 上工作正常
- r - 在 R 中过滤分组数据框时应用规则?
- node.js - Node JS、Socket.io、异步和阻塞事件循环
- php - 使用 Phar 提取无扩展名文件
- java - 重载包含不同路径变量类型的 RestController 方法
- spring - 限制 API 的每秒请求数
- php - 如果下一条记录中的 id 不同,如何创建一个新的
- android - 允许 beta 测试人员测试 google play 计费,但只针对一个应用?
- react-native - 部分有效:尝试在 React Native 中使用 componentDidMount 获取 AsyncStorage 对象