如何在 pandas 中使用 for 循环调用不同的数据文件?
python 319
原文标题 :How to call different data files using a for loop in pandas?
我有一个名为的文件列表,例如
topaccount_2015_09_individuals
topaccount_2015_12_indiviuuals
...
topaccount_2021_12_individuals
是的子集
topaccount_2015_09
topaccount_2015_12
...
topaccount_2021_12
我想打电话给他们并进行一些数据操作,所以我创建了一个列表,
known_series = known['Address']
y = ['2015_09', '2015_12', '2016_03', '2016_06', '2016_09', '2016_12', '2017_03', '2017_06', '2017_09', '2017_12',
'2018_03', '2018_06', '2018_09', '2018_12', '2019_03', '2019_06', '2019_09', '2019_12' , '2020_03', '2020_06', '2020_09', '2020_12',
'2021_03', '2021_03', '2021_06', '2021_09', '2021_12']
for q in y:
topaccount_[q]_individuals = topaccount_[q][~topaccount_[q]['address'].isin(known_series)]
topaccount_[q]_individuals = topaccount_[q]_individuals.reset_index(drop=True)
但它给了我一个错误。我究竟做错了什么? (known_series 已在脚本中定义)
更新我遵循了下面的建议,但我还有一个问题,即如何解决我从中提取 _individuals 数据帧的主数据帧。
y = ['2015_09', '2015_12', '2016_03', '2016_06', '2016_09', '2016_12', '2017_03', '2017_06', '2017_09', '2017_12',
'2018_03', '2018_06', '2018_09', '2018_12', '2019_03', '2019_06', '2019_09', '2019_12' , '2020_03', '2020_06', '2020_09', '2020_12',
'2021_03', '2021_03', '2021_06', '2021_09', '2021_12']
file_individuals = []
file = []
for x in y:
file_individuals.append(f'topaccount_{x}_individuals')
file.append(f'topaccount_{x}')
print(file_individuals)
print(file)
for file_individuals in file_individuals:
file_individuals = **topaccount_[q][~topaccount_[q]**['address'].isin(known_series)]
file_individuals = file_individuals[~file_individuals['address'].isin(coinmarketcap_series)]
file_individuals = file_individuals[~file_individuals['address'].isin(tord_series)]
file_individuals = file_individuals[~file_individuals['address'].isin(exchanges_series)]
file_individuals = file_individuals.reset_index(drop=True)
回复
我来回复-
Devang Sanghani 评论
然后像这样,稍后使用名单。
name = [] for x in y: name.append(f'topaccount_{x}_individuals') print(name) ['topaccount_2015_09_individuals', 'topaccount_2015_12_individuals', 'topaccount_2016_03_individuals', 'topaccount_2016_06_individuals', 'topaccount_2016_09_individuals', 'topaccount_2016_12_individuals', 'topaccount_2017_03_individuals', 'topaccount_2017_06_individuals', 'topaccount_2017_09_individuals', 'topaccount_2017_12_individuals', 'topaccount_2018_03_individuals', 'topaccount_2018_06_individuals', 'topaccount_2018_09_individuals', 'topaccount_2018_12_individuals', 'topaccount_2019_03_individuals', 'topaccount_2019_06_individuals', 'topaccount_2019_09_individuals', 'topaccount_2019_12_individuals', 'topaccount_2020_03_individuals', 'topaccount_2020_06_individuals', 'topaccount_2020_09_individuals', 'topaccount_2020_12_individuals', 'topaccount_2021_03_individuals', 'topaccount_2021_03_individuals', 'topaccount_2021_06_individuals', 'topaccount_2021_09_individuals', 'topaccount_2021_12_individuals']
2年前