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

5

5 Answers 5

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?

4
  • 1
    Will it be faster when I execute it with lot of data ? Sep 23, 2018 at 5:51
  • Yup it will, pandas is always fast Sep 23, 2018 at 5:52
  • 1
    Thank you sir, how to follow you ? Sep 23, 2018 at 6:00
  • @skjainmiah Follow me, there's no way to do that on stackoverflow tho, but an accept and upvote makes me happy already :-), hahaha. Sep 23, 2018 at 6:02
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])
2
  • But fruits names are in row wise how to make it column wise ? Sep 23, 2018 at 5:25
  • @skjainmiah transpose the dataframe i.e pd.DataFrame([fruits,prices]).T Sep 23, 2018 at 5:28
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.

1
  • 1
    Thanks for your answer but Column Names are missing Sir. Sep 23, 2018 at 5:36
1

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

1
  • When your answer is too short, you can send it as a comment. Sep 14, 2019 at 4:18
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.
5
  • Why zip instead of pandas T method Sep 23, 2018 at 5:38
  • When Pandas T is used we can transpose the data but column headings are not printed, while zip clearly represent the column names. Sep 23, 2018 at 5:42
  • Well but..., how about mine?, okay but then use index instead of columns, (all in mine) Sep 23, 2018 at 5:42
  • 1
    Thank you @U9-Forward . Your answer also works for me. Sep 23, 2018 at 5:49
  • happy that i helped,😊😊😤 Sep 23, 2018 at 5:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.