热线电话:13121318867

登录
2019-01-18 阅读量: 1269
Pandas提示错误:'Str'对象没有属性

问题描述:

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)

问题解决:

需要输入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')
13.4463
1
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子