Error while using rstrip in pandas - python

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(']')

Related

Replacing values in pandas dataframe using values in a list

I have a column in my df which ends with ['-A','-B','-T','-Z','-EQ','-BE','-BL','-BT','-GC','-IL','-IQ'], and I need to remove the values.
I tried the below and got an error
df['name'] = df['name'].str.replace(['-A','-B','-T','-Z','-EQ','-BE','-BL','-BT','-GC','-IL','-IQ'],'', regex=True)
TypeError: unhashable type: 'list'
Use Series.replace instead Series.str.replace:
df['name'] = df['name'].replace(['-A','-B','-T','-Z','-EQ','-BE','-BL','-BT','-GC','-IL','-IQ'],'', regex=True)

String object is not callable when creating a dataframe

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.

Pandas column dtype is object but python thinks it is float

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()]

Create a new column which is cast to a string in pandas

What would be the proper way to assign a stringified column to a dataframe, as I would like to keep the original so I don't want to use .astype({'deliveries': 'str'). SO far I have:
df = ( df.groupby('path')
.agg(agg_dict)
.assign(deliveries_str=df['deliveries'].str ??)
)
What would be the proper way to do this?
I also tried the following but I get an unhashable type error:
.assign(deliveries_str=lambda x: x.deliveries.str)
TypeError: unhashable type: 'list'
You need try change .str since it is a function
.assign(deliveries_str=lambda x: x.deliveries.astype(str))
Adding mask
.assign(deliveries_str=lambda x: x['deliveries'].astype(str).mask(x['deliveries'].isnull()))

AttributeError: Cannot access attribute 'str' of 'SeriesGroupBy' objects, try using the 'apply' method

i hv a dataframe that i got from groupby. now i need to convert the string in rows to separate columns. i used this :
df5 = pd.DataFrame([df4['notes'].str], columns = df4['id'])
print (df5.head())
but getting that error.

Categories