How can I access to an item in pandas series? - python

data=pd.read_csv('E:\\movie_data.csv')
indices=pd.Series(data.index, index=data['movie_title']).drop_duplicates
indices['Avatar']
This is the result
I've tried to access an element of this series,
But I received this error:
TypeError
----> 3 indices['Avatar']
TypeError: 'method' object is not subscriptable

You need to actually call (using the paranthesis) the drop_duplicates method to get a DataFrame in return. Try the following code and it should work :
data=pd.read_csv('E:\\movie_data.csv')
indices=pd.Series(data.index, index=data['movie_title']).drop_duplicates()
indices['Avatar']
For the record, the error message your are having is because you try to use object subscription (the bracket notation) on the drop_duplicates method not on the result of this method.

Related

Python .loc returning a TypeError [duplicate]

This question already has an answer here:
Pandas - DataFrame object is not callable
(1 answer)
Closed 1 year ago.
I'm new to using .loc , but every time I try to use it, it returns 'TypeError: 'DataFrame' object is not callable'.
For example, I can't get this simple code (used on the pokemon API) to work:
print(df.loc(df('Attack') > 175))
TypeError: 'DataFrame' object is not callable
Please can someone help, its really bugging me why it's not working ?!?
Indexing (both iloc and accessing column Attack) should be done with square bracket, not round bracket (as they are not functions)
For instance
df.loc[df['Attack'] > 175]
With df('Attack'), you are trying to call df object, which of course is not not a function and hence not callable

How to resolve the error 'tuple' object is not callable

I am new to python, I have imported a file into jupyter as follows:
df = pd.read_csv(r"C:\Users\shalotte1\Documents\EBQS_INTEGRATEDQUOTEDOCUMENT\groceries.csv")
I am using the following code to determine the number of rows and columns in the data
df.shape()
However I am getting the following error:
TypeError: 'tuple' object is not callable
You want df.shape - this will return a tuple as in (n_rows, n_cols). You are then trying to call this tuple as though it were a function.
As you are new to python, I would recommend you to read this page. This will make you get aware of other causes too so that you can solve this problem again if it appears in the future.
https://careerkarma.com/blog/python-typeerror-tuple-object-is-not-callable/

Getting " AttributeError: 'float' object has no attribute 'items' " with pandas.io.json.json_normalize

When trying to normalize a series within a pandas dataframe with the json_normalize function i am getting the Error:
"AttributeError: 'float' object has no attribute 'items'"
Each row of the series contains a nested json, though some rows don't contain all of the attributes, that are present in some of those json' from that series
also there is a field "timestamp":{"$date":1578411194000} within those nested json's, which is also present in another column of that same dataframe, giving me an error in another attempt to flatten that other series.
I am assuming the AttributeError has something to do with the either not all JSONs containing all the fields or sth. with those timestamps.json_normalize did work for some of the other df-columns.
I hope this is enough info. thanks a lot in advance!
It can happen if there are NaN fields which can be solved by using dropna() :
pd.json_normalize(df.explode("field")["field"]))
=> AttributeError: 'float' object has no attribute 'items'
pd.json_normalize(df.explode("field")["field"].dropna())
=> no error

getting 'DataFrameGroupBy' object is not callable in jupyter

I have this csv file from https://www.data.gov.au/dataset/airport-traffic-data/resource/f0fbdc3d-1a82-4671-956f-7fee3bf9d7f2
I'm trying to aggregate with
airportdata = Airports.groupby(['Year_Ended_December'])('Dom_Pax_in','Dom_Pax_Out')
airportdata.sum()
However, I keep getting 'DataFrameGroupBy' object is not callable
and it wont print the data I want
How to fix the issue?
You need to execute the sum aggregation before extracting the columns:
airportdata_agg = Airports.groupby(['Year_Ended_December']).sum()[['Dom_Pax_in','Dom_Pax_Out']]
Alternatively, if you'd like to ensure you're not aggregating columns you are not going to use:
airportdata_agg = Airports[['Dom_Pax_in','Dom_Pax_Out', 'Year_Ended_December']].groupby(['Year_Ended_December']).sum()

Pandas - DataFrame object is not callable

I have the following code:
df(df.Sex=='male')
I get an error stating that the DataFrame object is not callable.
How can I solve this?
It is called boolean indexing and need [] only:
df[df.Sex=='male']
Or:
df.query("Sex =='male'")

Categories

Resources