1
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

sk jainmiah
  • 53
  • 2
  • 2
  • 10

5 Answers5

1

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])
1

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:

See:What does it mean to "call" a function in Python?

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
1

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.

Suresh
  • 489
  • 3
  • 7
1

This error, i.e, 'type' object is not subscriptable, occurs when I am using square brackets. Using parenthesis solves the problem.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
0
    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.
sk jainmiah
  • 53
  • 2
  • 2
  • 10