在Pandas中确定DataFrame的周期索引和列
在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
course = ["DBMS", "DSA", "OOPS",
"System Design", "CN", ]
# pass the period and starting index
webinar_date = pd.period_range('2020-08-15', periods=5)
# Determine Period Index and Column
# for DataFrame
df = pd.DataFrame(course, index=webinar_date, columns=['Course'])
df
输出 :
示例 2:
import pandas as pd
day = ["Sun", "Mon", "Tue",
"Wed", "Thurs", "Fri", "Sat"]
# pass the period and starting index
daycode = pd.period_range('2020-08-15', periods=7)
# Determine Period Index and Column for DataFrame
df = pd.DataFrame(day, index=daycode, columns=['day'])
df
输出:
示例 3:
import pandas as pd
Team = ["Ind", "Pak", "Aus"]
# pass the period and starting index
match_date = pd.period_range('2020-08-01', periods=3)
# Determine Period Index and Column for DataFrame
df = pd.DataFrame(Team, index=match_date, columns=['Team'])
df
输出 :