I am trying to create a dataframe in pandas as follows:
cols = ['col1','col2']
df = pd.DataFrame(columns = cols)
I get the following error:
TypeError: 'str' object is not callable
Does anybody know the solution here?
Somewhere in your code you've overwritten the value for pd.DataFrame by assigning it a string value. Find and remove the offending line, restart your kernel, and try again.
Related
I read in a csv like this
df = pd.read_csv(self.file_path, dtype=str)
then I try this:
df = df[df["MY_COLUMN"].apply(lambda x: x.isnumeric())]
I get an AttributeError:
AttributeError: 'float' object has no attribute 'isnumeric'
Why is this happening? The column contains mostly digits.
I want to filter out the ones where there are no digits.
This question is not how to achieve that or do it better but why do I get an AttributeError here?
Why is this happening?
I think because NaN is not converting to string if use dtype=str, still is missing value, so type=float
Use Series.str.isnumeric for working isnumeric with missing values like all text functions in pandas:
df[df["MY_COLUMN"].str.isnumeric()]
I tried to code the program that allows the user enter the column and sort the column and replace the cell to the other entered information but I probably get syntact errors
I tried to search but I could not find any solution
import pandas as pd
data = pd.read_csv('List')
df = pd.DataFrame(data, columns = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O'])
findL = ['example']
replaceL = ['convert']
col = 'C';
df[col] = df[col].replace(findL, replaceL)
TypeError: Cannot compare types 'ndarray(dtype=float64)' and 'str'
I seems that your df[col] and findLand replaceLdo not have the same datatype. Try to run df[col] = df[col].astype(str) beofre you run df[col]=df[col].replace(findL, replaceL)and it should work
If the column/s you are dealing with has blank entries in it, you have to specify the na_filter parameter in .read_csv() method to be False.
That way, it will take all the column entries with blank/empty values as str and thus the not empty ones as str as well.
Doing the .replace() method using this will not give a TypeError as you will be parsing through both columns as strings and not 'ndarray(dtype=float64) and str.
I'm clueless about this error.
First I try
import pandas as pd
datafile = "E:\...\DPA.xlsx"
data = pd.read_excel(datafile)
data
And everything is fine. Then...
data.boxplot('DPA', by='Liga', figsize=(12, 8))
Everything keeps going fine. then...
ctrl = data['DPA'][data.group == 'PremierLeague']
grps = pd.unique(data.group.values)
d_data = {grp:data['DPA'][data.group == grp] for grp in grps}
k = len(pd.unique(data.group)) # number of conditions
N = len(data.values) # conditions times participants
n = data.groupby('Liga').size()[0] #Participants in each condition
And here is when I get this error:
AttributeError: 'DataFrame' object has no attribute 'group'
Any ideas? I'm following this steps https://www.marsja.se/four-ways-to-conduct-one-way-anovas-using-python/ to make an ANOVA.
Thank you.
DataFrame has no attribute group. However, it is possible to access data in a column in your dataframe with the same syntax used to access attributes and methods, i.e. if you have a column col, you may access the series related to this column through
df.col
What happened here is that your data is probably different from what she used in the tutorial. Or at least, the columns she has are different than the columns you have.
To solve that problem, you can either (I) simply rename your columns to match the columns from the tutorial or (II) replace data.group with the corresponding column name that you have in your df
I have a dataframe df with one of the column "values". It contains -
values
[u'12f4',u'ff45',u'tr23']
[u'125g4',u'ff145',u'trr523']
[u'12f34',u'ff2345',u'trg23a']
I want to remove ']' from each cell. I am using the following code -
df['values'] = df['values'].map(lambda x: x.rstrip(']'))
This gives me an error -
AttributeError: 'float' object has no attribute 'rstrip'
How do I get rid of this error?
Try use str.rstrip:
df['values'] = df['values'].str.rstrip(']')
I try to read data from a CSV file using the following command:
df = pandas.read_csv(finp, header=None, columns = cols)
As a result I get the following error message:
TypeError: parser_f() got an unexpected keyword argument 'columns'
I resolve the problem in the following way:
df = pandas.read_csv(finp, header=None)
df.columns = cols
But I still wonder why it did not worked the first way. Any ideas?
There is no parameter 'columns'. Use 'names' instead of it.
By the way, do you have a header rows in your CSV?