如何在Pandas中用频率确定周期范围
pandas.period_range()是Pandas的一个通用函数,用于返回一个固定频率的PeriodIndex,默认频率为日(日历)。
语法: pandas.period_range(start=None, end=None, periods=None, freq=None, name=None)
参数:
- start:生成周期的左边界
- end:生成周期的右边界
- periods: 产生的周期数
- freq :频率别名
- name : 产生的PeriodIndex的名称
返回:PeriodIndex
示例 1:
import pandas as pd
# initialize country
country = ["India", "Australia", "Pak", "Sri Lanka",
"England", "Bangladesh"]
# perform period_range() function
match_date = pd.period_range('8/1/2020', '8/6/2020', freq='D')
# generates dataframes
df = pd.DataFrame(country, index=match_date, columns=['Country'])
df
输出:
示例
import pandas as pd
# initialize country
Course = ["DSA", "OOPS", "DBMS", "Computer Network",
"System design", ]
# perform period_range() function
webinar_month = pd.period_range('8/1/2020', '12/1/2020', freq='M')
# generates dataframes
df = pd.DataFrame(Course, index=webinar_month, columns=['Course'])
df
输出:
示例 3:
import pandas as pd
# initialize gold price
gold_price = ["32k", "34k", "37k", "33k", "38k", "39k", "35k",
"32k", "42k", "52k", "62k", "52k", "38k", "39k",
"35k", "33k"]
# perform period_range() function
price_month = pd.period_range(start=pd.Period('2019Q1', freq='Q'),
end=pd.Period('2020Q2', freq='Q'),
freq='M')
# generates dataframes
df = pd.DataFrame(gold_price, index=price_month, columns=['Price'])
df
输出: