【发布时间】:2020-08-27 11:51:40
【问题描述】:
我有一个包含 10K 行的 excel 文件,每行都有一些推文信息。例如这些列:Tweet、Date of Tweet、User Name、Retweet Count、...、User Location、Sentiment(此列中的值是正面或负面或中性),State(该列的值为美国50个州),Abbreviation(该列的值为CA、NJ等州的缩写, NY,..), CountofNegative(此列为空,我希望在此列中写入每个州的负面推文数量,因此此列将有 50 个数字)。
问题:统计每个州或其缩写的负面推文数量,并写在 CountofNegative 列。 以下是我的代码:
import pandas as pd
file=pd.read_excel("C:/Users/amtol/Desktop/Project/filter.xlsx")
UserLocation= file["User Location"]
Sentiment= file["Sentiment"]
CountofNegative= file["CountofNegative"]
State=file["State"]
Abbreviation= file["Abbreviation"]
for i, (loc,sent) in enumerate(zip(UserLocation, Sentiment)):
count=0
for j, (state, abbr) in enumerate(zip(State, Abbreviation)):
if (loc == state or loc == abbr and sent == "Negative"):
count=count+1
file.loc[j+1,"CountofNegative"]=count
print(CountofNegative)
file.to_excel("C:/Users/amtol/Desktop/Project/filter.xlsx")
没有错误,但是在创建输出文件时,“CountofNegative”列的前 24 个值为零,其余为 1(它们不是正确答案)。另外,我想通过print(CountofNegative) 测试程序,但仍然没有发生任何事情。 (无输出)。如何修复我的代码?
【问题讨论】:
-
请provide a reproducible copy of the DataFrame with
df.to_clipboard(sep=',')。 Stack Overflow Discourages Screenshots。这个问题很可能会被否决。您不鼓励提供帮助,因为没有人愿意重新输入您的数据或代码,而且屏幕截图通常难以辨认。 -
或者您不想提供数据,或者如果数据太大,请演示一个看起来像真实数据的示例数据。
标签: python python-3.x excel pandas twitterapi-python