I have a few dataframes from an API set as variables shown in the list of data. When I tried to perform some functions the error shows:
AttributeError: 'list' object has no attribute 'loc'
data = ['dataA','dataB','dataC','dataD']
for i in data:
exec('{} = pd.DataFrame()'.format(i))
for i in data:
ma = 6
smaString = "SMA" + str(ma)
data[smaString] = data.iloc[:,3].rolling(window = ma).mean()
data = data.iloc[ma:]
Any help would be highly appreciated.
Thanks.
To answer your question, the error pops up because 'data' is not a dataframe but a list. 'iloc' or 'loc' functions cannot be used on a list.
In your question, you have shown the error message to have 'loc' whereas you have used 'iloc' in your code. These are two different things:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html
Also, it's unclear what you are trying to achieve from this code here.
Related
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/
I have a dictionary called sortdi with keys as integers and values as strings. I want to store the values into a dataset so I created the following code
sortdi = sortFreqDict(dictonary)
dataset = sortdi.values()
When I try to run the code, the error that shows up is ,
AttributeError: 'list' object has no attribute 'values'
What do I do to store values into the dataset ?
It looks like the sortFreqDict function returns a list not a dictionary. To add to a list you use list.append().
Try printing sortdi and see if that gives you what you were hoping for?
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
I have a dataframe and I want to make a deep copy of it so I can modify the copy and use it in further processing.
I am working in Azure Databricks.
My dataframe is called "a" and I tried the following command:
b = a.copy(deep=True)
When I run it, I encounter the following error :
'DataFrame' object has no attribute 'copy'
I also tried to use 'iloc' or 'loc' function to create a new dataframe only with the columns that I need, but same error ('DataFrame' object has no attribute 'lit').
Any ideas why is this happening?..
Assuming you're working in Python, check whether you're using a Spark DataFrame or a pandas DataFrame. If you're using a pandas one then I couldn't tell you what's going on without more information; if you're using the spark one then you should use
newDataFrame = oldDataFrame.select('*')
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()