gpt4 book ai didi

python - 在 Pandas 中使用 For 循环的数据框

转载 作者:行者123 更新时间:2023-12-01 09:28:17 24 4
gpt4 key购买 nike

我需要运行这样的代码行。

tweet24042018 = tweets.loc[tweets['date2'] == '24042018'].copy()
tweet23042018 = tweets.loc[tweets['date2'] == '23042018'].copy()
tweet22042018 = tweets.loc[tweets['date2'] == '22042018'].copy()

创建并尝试的函数是这样的,

for key in collect:
['tweet'+f'{key}'] = tweets.loc[tweets['date2'] == f'{key}'].copy()

但是,它会给出类似的错误,

File "<ipython-input-18-3c946d8cc61a>", line 2
['tweet'+f'{key}'] = tweets.loc[tweets['date2'] == f'{key}'].copy()
^
SyntaxError: can't assign to operator

请帮忙

最佳答案

尝试使用本地人

variables = locals()
for key in collect:
variables["tweet{0}".format(key)]= tweets.loc[tweets['date2'] == key]
print(variables["tweet{0}".format(key)].head())

关于python - 在 Pandas 中使用 For 循环的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50185547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com