如何结合pandas和dataclasses导入数据

huangapple go评论70阅读模式
英文:

How to combine pandas, dataclasses to import data

问题

我正在尝试使用dataclasses学习类

@dataclass
class Collect:
    url : str
    def collect(url):    
        df = pd.read_csv(url)
        return df
df = Collect("national_news_scrape.csv")
df.head()

但是我遇到了错误:

AttributeError: 'Collect'对象没有'head'属性

不太清楚为什么。

英文:

I'm trying to learn classes with dataclasses

@dataclass
class Collect:
    url : str
    def collect(url):    
        df = pd.read_csv(url)
        return df
df = Collect("national_news_scrape.csv")
df.head()

But I get error:

AttributeError: 'Collect' object has no attribute 'head'

Not sure why.

答案1

得分: 1

以下是示例:

@dataclass
class Collect:
    url: str

    def collect(self):
        df = pd.read_csv(self.url)
        return df

# 创建一个 Collect 类的实例
instance = Collect("national_news_scrape.csv")
print(instance.url)  # national_news_scrape.csv

# 调用 Collect 类的方法
df = instance.collect()
print(df.head())
英文:

Here is an example:

@dataclass
class Collect:
    url: str

    def collect(self):    
        df = pd.read_csv(self.url)
        return df

# create an instance of Collect class
instance = Collect("national_news_scrape.csv")
print(instance.url)  #  national_news_scrape.csv

# call the method of Collect class
df = instance.collect()
print(df.head())

答案2

得分: 1

问题的原因

你收到AttributeError是因为你的代码构造了Collect对象,并使用了Collect("national_news_scrape.csv")但是没有调用collect()方法来返回一个pandas的DataFrame

head()方法是在DataFrame实例上定义的,而不是在你的Collect类的实例上。

修复方法

然而,我感到有些混淆!我会假设你的代码是一个_最小(不-)工作示例_,这解释了为什么你在这里使用了@dataclass,当它似乎不是必要的时候。

Danila发布了一些有效的代码,尽管我自己没有测试过 - 请查看这个答案以获取修正。

此外,请注意collect()方法的签名变化 - 现在不再需要一个url位置参数,因为它使用了由构造函数从@dataclasss设置的self.url实例属性。

英文:

The cause

You are getting the AttributeError because your code constructs the Collect object with Collect("national_news_scrape.csv"), but does not then call the collect() method to return a pandas DataFrame.

The head() method is defined on a DataFrame instance and not an instance of your Collect class.

The fix

I do however sense some confusion! I will assume that your code is a Minimal (non-)Working Example, which would explain why you are using @dataclass here when it doesn't seem necessary.

Danila has posted some working code, though I haven't tested it myself - please consult this answer for a correction.

Additionally, please note the change in signature of the collect() method - it now no longer requires a url positional argument because it uses the self.url instance attribute that is set by the constructor from @dataclasss.

huangapple
  • 本文由 发表于 2023年7月20日 16:50:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定