In this dictionary, im getting error "ValueError: All arrays must be of the same length" can we repeat month name in month column
Question:
This is a dictionary which i want to convert into a data frame but getting error "ValueError: All arrays must be of the same length"
I want to try dataframe like in which year 2014 repeated in year column till the end and monthName column also repeated value in column like january,february,March,january,February so on, how can i do that ? is it possible to do that? Please help me
Dict_tab = {'Year': '2014',
'monthName': ['January', 'February', 'March'],
'Weekend': ['5','12','19','26','2','9','16','23','2','9','16','23','30'],
'Avg': ['2.3','2','1','5','3','str','none','3','5','2','1','78','5']}
dataframe = pd.DataFrame(Dict_tab)
print(dataframe)
Answers:
By creating a DataFrame
using pandas, you are creating a table. For this reason, all columns must have the same number of values. The only exception to this is singular values, where it will be assumed that every value in that column will be the same. Because you provided 13 weekends and 13 averages but only 3 months, pandas doesn’t know what the other 10 months are.
To solve this, you need to repeat the months so that all rows have a month provided.
For example, by assuming that the weekend increases for the month and decreases when going to the next month, you can assume that monthName
should have the following assignment:
'monthName': ['January', 'January', 'January', 'January', 'February', 'February', 'February', 'February', 'March', 'March', 'March', 'March', 'March'],
This leads to the following output:
Year monthName Weekend Avg
0 2014 January 5 2.3
1 2014 January 12 2
2 2014 January 19 1
3 2014 January 26 5
4 2014 February 2 3
5 2014 February 9 str
6 2014 February 16 none
7 2014 February 23 3
8 2014 March 2 5
9 2014 March 9 2
10 2014 March 16 1
11 2014 March 23 78
12 2014 March 30 5
This is a dictionary which i want to convert into a data frame but getting error "ValueError: All arrays must be of the same length"
I want to try dataframe like in which year 2014 repeated in year column till the end and monthName column also repeated value in column like january,february,March,january,February so on, how can i do that ? is it possible to do that? Please help me
Dict_tab = {'Year': '2014',
'monthName': ['January', 'February', 'March'],
'Weekend': ['5','12','19','26','2','9','16','23','2','9','16','23','30'],
'Avg': ['2.3','2','1','5','3','str','none','3','5','2','1','78','5']}
dataframe = pd.DataFrame(Dict_tab)
print(dataframe)
By creating a DataFrame
using pandas, you are creating a table. For this reason, all columns must have the same number of values. The only exception to this is singular values, where it will be assumed that every value in that column will be the same. Because you provided 13 weekends and 13 averages but only 3 months, pandas doesn’t know what the other 10 months are.
To solve this, you need to repeat the months so that all rows have a month provided.
For example, by assuming that the weekend increases for the month and decreases when going to the next month, you can assume that monthName
should have the following assignment:
'monthName': ['January', 'January', 'January', 'January', 'February', 'February', 'February', 'February', 'March', 'March', 'March', 'March', 'March'],
This leads to the following output:
Year monthName Weekend Avg
0 2014 January 5 2.3
1 2014 January 12 2
2 2014 January 19 1
3 2014 January 26 5
4 2014 February 2 3
5 2014 February 9 str
6 2014 February 16 none
7 2014 February 23 3
8 2014 March 2 5
9 2014 March 9 2
10 2014 March 16 1
11 2014 March 23 78
12 2014 March 30 5