我正在使用Pytrends,我想循环遍历数据框架的列,但我得到了错误:"无法将系列转换为'int'类>"。我不知道问题在哪里,我已经检查了数据框架,我使用的列是int类型的。
index ICO_Name ticker ico_launch_month year month day
4 micromoney amm 2017-10-18 00:00:00 2017 10 18
3 worldcore nan 2017-10-14 00:00:00 2017 10 14
2 xave coin xvc 2022-11-01 00:00:00 2022 11 1
1 sf capital sfcp 2018-01-06 00:00:00 2018 1 6
0 cryptostone cps 2022-06-13 00:00:00 2022 6 13
<class 'pandas.core.frame.DataFrame'>
Int64Index: 199 entries, 0 to 198
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 ICO_Name 199 non-null object
1 ticker 199 non-null object
2 ico_launch_month 199 non-null datetime64[ns]
3 year 199 non-null int64
4 month 199 non-null int64
5 day 199 non-null int64
dtypes: datetime64[ns](1), int64(3), object(2)
memory usage: 10.9+ KB
代码:
pytrend = TrendReq()
for id in df_full_1:
g_trends = pytrend.get_historical_interest(df_full_1["ICO_Name"], year_start=df_full_1["year"], month_start=df_full_1["month"],
day_start=df_full_1["day"], year_end= df_full_1["year"],
month_end=df_full_1["month"], day_end=df_full_1["day"], sleep=0).drop(columns='isPartial')
错误:
TypeError Traceback (most recent call last)
<ipython-input-56-e3cb7e0b950c> in <module>()
4 g_trends = pytrend.get_historical_interest(df_full_1["ICO_Name"], year_start=df_full_1["year"], month_start=df_full_1["month"],
5 day_start=df_full_1["day"], year_end= df_full_1["year"],
----> 6 month_end=df_full_1["month"], day_end=df_full_1["day"], sleep=0).drop(columns='isPartial')
7
8 g_trends
1 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/series.py in wrapper(self)
183 if len(self) == 1:
184 return converter(self.iloc[0])
--> 185 raise TypeError(f"cannot convert the series to {converter}")
186
187 wrapper.__name__ = f"__{converter.__name__}__"
TypeError: cannot convert the series to <class 'int'>
我认为您应该回到文档中检查函数get_historical_interest的参数类型,您应该将列设置为整数值,例如:
pytrend.get_historical_interest(..., year_start=2020, month_start=10, day_start=1, hour_start=0, year_end=2021, month_end=10, day_end=1,...)
因为df_full_1["day"]
是一个级数,而函数期望得到一个整数而不是一个级数。