添加到 DataFrame 时无法连接对象

发布于 2025-01-10 06:03:52 字数 1597 浏览 0 评论 0原文

我正在尝试向 DataFrame 添加一个句子和一枚硬币(我猜在本例中就像一个标签)。虽然我不断收到此错误:

Traceback (most recent call last):
      File "c:\Users\gjohn\Documents\code\machineLearning\trading_bot\filter.py", line 132, in <module>
        df = df.append({'coin': coin, 'review': sentence}, ignore_index=True)
      File "C:\Users\gjohn\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\series.py", line 2877, in append
        return concat(
      File "C:\Users\gjohn\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
        return func(*args, **kwargs)
      File "C:\Users\gjohn\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\reshape\concat.py", line 294, in concat
        op = _Concatenator(
      File "C:\Users\gjohn\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\reshape\concat.py", line 384, in __init__
        raise TypeError(msg)
 TypeError: cannot concatenate object of type '<class 'dict'>'; only Series and DataFrame objs are valid

这是代码:

data = pd.read_csv('C:\\Users\\gjohn\\Documents\\code\\machineLearning\\trading_bot\\testreviews.csv')
df = data['review'] # Create a dataframe of the reviews.
classes = data['class'] # Create a dataframe of the classes.
for sentence in sentences:
    coin = find_coin(common_words, sentence)
    if len(sentence) > 0 and coin != None:
        df = df.append({'coin': coin, 'review': sentence}, ignore_index=True)

我找不到如何解决此问题并且我确实需要帮助,但如果您能帮助我,那就太好了。谢谢!

也对混乱的代码表示歉意:D

I am trying to add a sentence as well as a coin(like a label in this case I guess) to a DataFrame. Although I keep getting this error:

Traceback (most recent call last):
      File "c:\Users\gjohn\Documents\code\machineLearning\trading_bot\filter.py", line 132, in <module>
        df = df.append({'coin': coin, 'review': sentence}, ignore_index=True)
      File "C:\Users\gjohn\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\series.py", line 2877, in append
        return concat(
      File "C:\Users\gjohn\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
        return func(*args, **kwargs)
      File "C:\Users\gjohn\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\reshape\concat.py", line 294, in concat
        op = _Concatenator(
      File "C:\Users\gjohn\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\reshape\concat.py", line 384, in __init__
        raise TypeError(msg)
 TypeError: cannot concatenate object of type '<class 'dict'>'; only Series and DataFrame objs are valid

Here is the code:

data = pd.read_csv('C:\\Users\\gjohn\\Documents\\code\\machineLearning\\trading_bot\\testreviews.csv')
df = data['review'] # Create a dataframe of the reviews.
classes = data['class'] # Create a dataframe of the classes.
for sentence in sentences:
    coin = find_coin(common_words, sentence)
    if len(sentence) > 0 and coin != None:
        df = df.append({'coin': coin, 'review': sentence}, ignore_index=True)

I can't find how to fix this and I really need help, it would be great if you could help me out. Thanks!

Also sorry for the messy code :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗷兮 2025-01-17 06:03:52

您用来构造字典的句子是什么?

也许您应该检查字典是否构建正确?

What is the sentence you use to construct the dictionary?

Perhaps you should check if the dictionary is constructed correctly?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文