0

Consider the following: start_date = input('Enter starting date in Month-Year format :') end_date = input('Enter ending date in Month-Year format :') course = 'English'

if the user enter 01-2022 and 05-2022, the list of input will be the following: ['01-2022','05-2022','English'] I want to have a dataframe that looks like this:

ID Date Course
1 Jan-2022 English
2 Feb-2022 English
3 Mar-2022 English
4 Apr-2022 English
5 May-2022 English

If a new user provides new input such as ['01-2022','01-2022','Math']

I want to append this to the previous dataframe and have

ID Date Course
1 Jan-2022 English
2 Feb-2022 English
3 Mar-2022 English
4 Apr-2022 English
5 May-2022 English
6 Jan-2022 Math

First, I tried generating a list of dates based on this date range with:

month_list = pd.date_range('2014-10-10','2016-01-07', freq='MS').strftime("%b-%Y").tolist()

the result was:

['Nov-2014', 'Dec-2014', --- 'Dec-2015', 'Jan-2016']

then wanted to convert this to a list of lists and append the course name before creating the dataframe from the new list.

Any help?

  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Semo Nov 25 '22 at 08:09
  • https://stackoverflow.com/questions/28006793/pandas-dataframe-to-list-of-lists could at least answer to create a list from lists in python with pandas. – Semo Nov 25 '22 at 08:15

1 Answers1

0

If you have a table .

ID  Date    Course
1   Jan-2022    English
2   Feb-2022    English
3   Mar-2022    English
4   Apr-2022    English
5   May-2022    English
6   Jan-2022    Math 

you want to append new values at the end of the table then you can create a table for the new values with same column names and then concatenate the two tables. For example if you have a month_list = ['Nov-2014', 'Dec-2014', --- 'Dec-2015', 'Jan-2016']

then create a new table that with subject

# original_df = your original table
# subject = subject entered by the user
# month_list = month list created based on the values entered by the user.

# Create the dataframe
df = pd.DataFrame({'Date': month_list, 'Course': [subject]* len(month_list)}, index=np.arange(len(original_df) + 1, len(month_list) + len(original_df) + 1))

# Concatenate the two dataframes
original_df = pd.concat([original_df, df])
Daud Khan
  • 11
  • 3