This is my code:
users.age.mean().astype(int64)
(where users is the name of dataframe and age is a column in it)
This is the error I am getting:
AttributeError
Traceback (most recent call last)
<ipython-input-29-10b672e7f7ae> in <module>
----> 1 users.age.mean().astype(int64)
AttributeError: 'float' object has no attribute 'astype'
users.age.mean() returns a float not a series. Floats don't have astype, only pandas series.
Try:
x = numpy.int64(users.age.mean())
Or:
x = int(users.age.mean())
Try int before your function example:
X = int(users.age.mean())
Hope it helps!
Related
I am trying to change a object column to a date time column.
However, every time I run the code below, I get an TypeError: "NATType" object is not callable.
I am assuming that this is due to the blanks in the column but I am not really sure how to resolve it. Removing the rows is not an option here because there are other columns to consider as well.
df['jahreskontakt'] = pd.to_datetime(df['jahreskontakt'], errors='ignore')
Does anybody have any advice? Thanks in advance.
Explanations:
df['jahreskontakt'] #column with yearly contacts by sales team
Values that can be found in the column:
2014-07-01 00:00:00
00:00:00
""
Full error: Tried changing between error = coerce or ignore
TypeError Traceback (most
recent call last)
<ipython-input-122-9d57805d9290> in <module>()
----> 1 df['jahreskontakt'] = pd.to_datetime(df['jahreskontakt'], errors='coerce')
TypeError: 'NaTType' object is not callable
Given a Mysql table with columns ("Title", "Author", "Date"). How do you:
Iterate over database to compare a given user provided date input to the database column "Date"
append matching records to lists
without getting the error "TypeError: argument of type 'datetime.date' is not iterable" example code below: Python 3.7
date = request.form.get("date")
list1=[]
list2=[]
list3=[]
results = db.session.query(Books).all()
for i in results:
if date in i.date is True:
list1.append(i.title)
list2.append(i.author)
list3.append(i.date)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-28-c4085a31faa3> in <module>()
5 results = db.session.query(Books).all()
6 for i in results:
----> 7 if date in i.date:
8 list1.append(i.title)
9 list2.append(i.author)
TypeError: argument of type 'datetime.date' is not iterable
Use sqlachemy filter to search. Doing database operations in application code is comparatively poor performance.
results = db.session.query(Books).filter(Books.date==date)
When I want to remove some elements which satisfy a particular condition, python is throwing up the following error:
TypeError Traceback (most recent call last)
<ipython-input-25-93addf38c9f9> in <module>()
4
5 df = pd.read_csv('fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv;
----> 6 df = filter(df,~('-02-29' in df['Date']))
7 '''tmax = []; tmin = []
8 for dates in df['Date']:
TypeError: 'int' object is not iterable
The following is the code :
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv');
df = filter(df,~('-02-29' in df['Date']))
What wrong could I be doing?
Following is sample data
Sample Data
Use df.filter() (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.filter.html)
Also please attach the csv so we can run it locally.
Another way to do this is to use one of pandas' string methods for Boolean indexing:
df = df[~ df['Date'].str.contains('-02-29')]
You will still have to make sure that all the dates are actually strings first.
Edit:
Seeing the picture of your data, maybe this is what you want (slashes instead of hyphens):
df = df[~ df['Date'].str.contains('/02/29')]
Seen a couple of answers to the general question, and I've used some of the solutions suggested, but still getting stuck.
I have the following code:
name = ['Sepal-length', 'Sepal-width', 'Petal-length', 'Petal-width', 'Class']
iris_ds = pd.read_csv(url, names=name)
cols=iris_ds.columns.drop('Class')
iris_ds[cols]=iris_ds[cols].apply(pd.to_numeric, errors='coerce')
.......
iris_ds['Sepal-area'] = iris_ds.eval('Sepal-width' * 'Sepal-length')
print(iris_ds.head(20))
However, when I run the script for the second section, I get the following:
Traceback (most recent call last): File "Iris_Data_set1.py", line
67, in
iris_ds['Petal-area'] = iris_ds.eval('Petal-width' * 'Petal-length') TypeError: can't multiply sequence by non-int of type
'str'
The data types are as follows:
Sepal-length float64
Sepal-width float64
Petal-length float64
Petal-width float64
Class object
dtype: object
Any suggestions on how to resolve this issue, so that I can do the multiplication?
Is there any reason why you can't just do:
iris_ds['Sepal-area'] = iris_ds.Sepal-width * iris_ds.Sepal-length
I think there might be 2 problems though. You probably shouldn't be using Sepal-length as a column name and instead should use Sepal_length (and apply this to your other variables), making the answer:
iris_ds['Sepal_area'] = iris_ds.Sepal_width * iris_ds.Sepal_length
I want to get the numpy.mean of each column of my pandas.DataFrame.
Here is my code:
import pandas as pd
import numpy as np
prices = pd.DataFrame([[-0.33333333, -0.25343423, -0.1666666667],
[+0.23432323, +0.14285714, -0.0769230769],
[+0.42857143, +0.07692308, +0.1818181818]])
print(np.mean(prices, axis=0))
If I run this code, I'll get the following error:
Traceback (most recent call last):
File "C:\Users\*****\Documents\Python\******\****.py", line 8, in <module>
print(np.mean(new, axis=0))
File "C:\Python33\lib\site-packages\numpy\core\fromnumeric.py", line 2711, in mean
return mean(axis=axis, dtype=dtype, out=out)
TypeError: mean() got an unexpected keyword argument 'dtype'
How can I fix that?
NOTE: here is an expected output: pd.DataFrame([0.1098537767, -0.0112180033, -0.0205905206])
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.mean.html
>>> prices.mean(axis=0)
0 0.109854
1 -0.011218
2 -0.020591
dtype: float64
>>> type(prices.mean(axis=0))
<class 'pandas.core.series.Series'>
If you wanted a DataFrame instead of a Series:
price_means = pd.DataFrame(prices.mean(axis=0))