Generation

generate functionWed, 01 Feb 2023

There is a Pandas dataframe: song performer chart_debut peak_position worst_position time_on_chart consecutive_weeks 450 (Can't Live Without Your) Love And Affection Nelson 1990-07-07 1 93 15 14.0 607 (Everything I Do) I Do It For You Bryan Adams 1991-06-29 1 53 9 8.0 748 (Hey Won't You Play) Another Somebody Done Som... B.J. Thomas 1975-02-01 1 99 17 16.0 852 (I Can't Get No) Satisfaction The Rolling Stones 1965-06-12 1 67 13 12.0 951 (I Just) Died In Your Arms Cutting Crew 1987-03-07 1 80 14 13.0 Create a new dfs dataframe, where the data format of the chart_debut column is changed from 1991-06-29 to 1991

import pandas as pd # create a new dataframe dfs = df[['song','performer','chart_debut','peak_position','worst_position','time_on_chart','consecutive_weeks']] # convert the chart_debut to string dfs['chart_debut'] = dfs['chart_debut'].astype(str) # get the date date = dfs.chart_debut.str.split("-", n = 1, expand = True) # drop the chart_debut dfs.drop(columns =['chart_debut'], inplace = True) # insert the date dfs.insert(2,'chart_debut',date[0]) # show the top 5 rows print(dfs.head())

Questions about programming?Chat with your personal AI assistant