【问题标题】:how to pass a column with error: Index(...) must be called with a collection of some kind, Timestamp('2021-03-02 05:45:42') was passed in python如何传递有错误的列:必须使用某种集合调用 Index(...),Timestamp('2021-03-02 05:45:42') 在 python 中传递
【发布时间】:2021-05-31 18:00:20
【问题描述】:

我想创建一个根据指定关键字变量显示内容的折线图。我通过获取 csv 文件中的数据来做到这一点,该文件包含以下列:关键字、日期、推文、情绪。当我使用if函数检查Keyword =字段(指定的关键字变量)的内容是否有错误:

TypeError: Index(...) 必须使用某种集合调用,Timestamp('2021-03-02 05:45:42') 已通过。

我不知道我需要修复哪个部分。 这是我的代码:

query = "bali"
df = pd.read_csv('tes3.csv', header=None, 
                 names=["Keyword", "Date", "Tweet", "Sentimen"])
df['Date'] = pd.to_datetime(df['Date'], errors='coerce')


for index, row in df.iterrows():
    if row['Keyword']==query:
        result = row.groupby([pd.Grouper(key='Date', freq='3s'), 'Sentimen']).count().unstack(fill_value=0).stack().reset_index()
        

【问题讨论】:

    标签: python pandas csv


    【解决方案1】:

    我认为这里不需要循环,通过boolean indexing过滤然后使用groupby

    mask = df['Keyword']==query
    result = df[mask].groupby([pd.Grouper(key='Date', freq='3s'), 'Sentimen']).count().unstack(fill_value=0).stack().reset_index()
    

    【讨论】:

      猜你喜欢
      • 2022-10-18
      • 2020-12-02
      • 2022-06-15
      • 2016-12-15
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多