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?
Related
I am importing a CSV file and I want to look at the school_hours column and extract the starting hours. Here are the first few lines from the column (you can assume that the data is already imported)
Essentially, I want to create a new column named starting_hours that should look something like this:
This is the following code that I wrote:
df['starting_hours']= [i[0:5] for i in df.School_Hours.str.split(' ').values]
I keep getting a 'float' object is not subscriptable error
You can try str.split then access the first element of list with str[0]
df['starting_hours'] = df['School_Hours'].str.split('-').str[0].str.strip('AM|PM')
print(df)
School_Hours starting_hours
0 08:00AM-O3:00PM 08:00
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.
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/
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 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()