gpt4 book ai didi

python - Pandas 'Str' 对象没有属性 'to_csv'

转载 作者:太空宇宙 更新时间:2023-11-04 02:07:54 24 4
gpt4 key购买 nike

目前,我想知道为什么 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)

DataFrame 的输出

    Title         Search_Score    Country Code     Platform        
アジアカップ 2019 20000 JP Google
康華 2000 HK Google
스피릿위시 2000 KR Google
Michelle Obama 50000 US Google

Updated ( Include 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')

关于python - Pandas 'Str' 对象没有属性 'to_csv',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54248662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com