Reputation: 53
import pandas as pd
import numpy as np
fruits = ["APPLE","BANANA","GRAPES","ORANGE"]
prices = [80,45,60,50]
fru_prices = pd.DataFrame[fruits,prices]
I am getting error while I am creating a Data Frame : 'type' object is not subscriptable
Upvotes: 1
Views: 20255
Reputation: 11
This error, i.e, 'type' object is not subscriptable, occurs when I am using square brackets. Using parenthesis solves the problem.
Upvotes: 1
Reputation: 53
import pandas as pd
import numpy as np
fruits = ["APPLE","BANANA","GRAPES","ORANGE"]
prices = [80,45,60,50]
fru_prices = list(zip(fruits,prices))
pd.DataFrame(data = fru_prices ,columns = ['fruits','prices'])
---------------------------------------------------------------------
output :
fruits prices
0 APPLE 80
1 BANANA 45
2 GRAPES 60
3 ORANGE 50
> Python zip function takes iterable elements as input, and returns
> iterator. Now data will appear in column wise.
Upvotes: 0
Reputation: 71570
You have to call DataFrame
then as an argument do the list:
fru_prices=pd.DataFrame([fruits,prices])
And as you want to transpose do:
fru_prices=fru_prices.T
And need columns so:
fru_prices.columns=['fruits','prices']
Then fru_prices
is what you want
Actually you can do this all in one-line:
fru_prices=pd.DataFrame([fruits,prices],index=['fruit','prices']).T
Related:
Upvotes: 1
Reputation: 489
Use below code as: pd.DataFrame is a method. python : 3.6.2
import pandas as pd
import numpy as np
fruits = ["APPLE","BANANA","GRAPES","ORANGE"]
prices = [80,45,60,50]
fru_prices =pd.DataFrame([fruits,prices])
fru_prices = fru_prices.set_index(0).T ##This entry conver it into row to column
print(fru_prices)
OutPut:
0 APPLE 80
1 BANANA 45
2 GRAPES 60
3 ORANGE 50
Check this and let us know in case of this code works.
Upvotes: 1
Reputation: 404
pd.DataFrame
is a method and of type 'type'
. So, you are getting error as 'type' object is not subscriptable
. So,
fru_prices = pd.DataFrame([fruits,prices])
Upvotes: 1