Python 将数据类型:句点[M]转换为字符串格式
我已将日期转换为Dtype M格式,因为我不想与日期有任何关系。不幸的是,我不能用这种格式打印,所以我现在想把它转换成字符串Python 将数据类型:句点[M]转换为字符串格式,python,pandas,numpy,Python,Pandas,Numpy,我已将日期转换为Dtype M格式,因为我不想与日期有任何关系。不幸的是,我不能用这种格式打印,所以我现在想把它转换成字符串 df['Date_Modified'] = pd.to_datetime(df['Collection_End_Date']).dt.to_period('M') #Add a new column called Date Modified to show just month and year df = df.groupby(["Date_Modified", "
df['Date_Modified'] = pd.to_datetime(df['Collection_End_Date']).dt.to_period('M')
#Add a new column called Date Modified to show just month and year
df = df.groupby(["Date_Modified", "Entity"]).sum().reset_index()
#Group the data frame by the new column and then Company and sum the values
df["Date_Modified"].index = df["Date_Modified"].index.strftime('%Y-%m')
所以我需要对我的数据进行分组,这样我可以按月打印出一些图表。
但是当我的数据位于dtype:Mperiod
所以我想把它转换成字符串
df['Date_Modified'] = pd.to_datetime(df['Collection_End_Date']).dt.to_period('M')
#Add a new column called Date Modified to show just month and year
df = df.groupby(["Date_Modified", "Entity"]).sum().reset_index()
#Group the data frame by the new column and then Company and sum the values
df["Date_Modified"].index = df["Date_Modified"].index.strftime('%Y-%m')
它返回一个数字字符串,但我需要它来返回字符串输出。用于上一步中的字符串设置系列:
df["Date_Modified"]= df["Date_Modified"].dt.strftime('%Y-%m')
或者将其设置在groupby
之前,则无需转换为月周期:
df['Date_Modified'] = pd.to_datetime(df['Collection_End_Date']).dt.strftime('%Y-%m')
df = df.groupby(["Date_Modified", "Entity"]).sum().reset_index()