简体   繁体   English

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

[英]Pandas 'Str' object has no attribute 'to_csv'

At the moment, I'm wondering why is Pandas not able to convert the dataframe to a csv file as it returns AttributeError: 'str' object has no attribute 'to_csv'目前,我想知道为什么 Pandas 无法将数据帧转换为 csv 文件,因为它返回AttributeError: 'str' object has no attribute 'to_csv'

I've been trying trends.to_string(index=False).to_csv('test.csv')) etc and a few other examples others have given, but it returns the same thing over and over.我一直在尝试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)

Output of 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 )更新(包括main

You probably want, the below code, you have to enter the argument of trends method:你可能想要,下面的代码,你必须输入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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM