in Education by
In the code below I pivot a dataframe using an index date. After the pivot, I need to get the month from column date. This is my attempt: df = pd.DataFrame({ 'date' : [datetime(2021,3,11), datetime(2021,3,11), datetime(2021,3,11), datetime(2021,3,12), datetime(2021,3,12), datetime(2021,3,12), datetime(2021,3,13), datetime(2021,3,13), datetime(2021,3,13)], 'field': ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C'], 'value': [150, 140, 1, 130, 280, 2, 260, 120, 4] }) print(df) date field value 0 2021-03-11 A 150 1 2021-03-11 B 140 2 2021-03-11 C 1 3 2021-03-12 A 130 4 2021-03-12 B 280 5 2021-03-12 C 2 6 2021-03-13 A 260 7 2021-03-13 B 120 8 2021-03-13 C 4 df_pivoted = df.pivot(index='date', columns='field', values='value') print(df_pivoted) field A B C date 2021-03-11 150 140 1 2021-03-12 130 280 2 2021-03-13 260 120 4 df_pivoted['month'] = df_pivoted['date'].apply(lambda x: x.month) I get an error, as the date field is not a column, but I need to use it: KeyError: 'date' Why can't I use a column that's also an index? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
date is the index, so you have to get the months from the index: df_pivoted['month'] = df_pivoted.index.month Output: field A B C month date 2021-03-11 150 140 1 3 2021-03-12 130 280 2 3 2021-03-13 260 120 4 3

Related questions

0 votes
    I have some columns that have the same names. I would like to add a 1 to the repeating column ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    Problem: Select first N rows and last row in Python Pandas. only gets 1 row which is index 9 and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    So I have a list of people, each of them are given more than 2 books, 4 books are possible. I ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    How do I get the index column name in python pandas? Here's an example dataframe: Index Title Column 1 Apples 1 ... how to do this? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I am trying to create a pandas dataframe that combines all children into one row class Parent(Base): ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    In [60]: print(row.index) Int64Index([15], dtype='int64') I already know that the row number is ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    To the data frame df: Player Team Points Mean Price Value Gameweek 1 Jim Leeds 4.4 4.40 10.44 0.44 2 Jim ... scalar What am I missing? Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    I have one Dataframe. Dataframe1: desc id result A 1 Yes A 2 No A 3 Yes A 4 No B 1 No ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 11, 2022 in Education by JackTerrance
0 votes
    I have one Dataframe. Dataframe1: desc id result A 1 Yes A 2 No A 3 Yes A 4 No B 1 No ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have one Dataframe. Dataframe1: desc id result A 1 Yes A 2 No A 3 Yes A 4 No B 1 No ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    i have multiple excel files with uniform column names, except for one. One file calls it EndOfMarchStatus, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    Working with census data, I want to replace NaNs in two columns ("workclass" and "native-country") with the ... way to do this? Select the correct answer from above options...
asked Feb 3, 2022 in Education by JackTerrance
0 votes
    I have a dataframe as below itm Date Amount 67 420 2012-09-30 00:00:00 65211 68 421 2012-09-09 00 ... solutions would be appreciated. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I want to get a list of the column headers from a pandas DataFrame. The DataFrame will come from user input so I don ... 'gdp', 'cap'] Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    How to delete a column in a DataFrame, currently I am using: del df['column_1'] this is working fine, ... expected df.column_name ? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
...