Python '<' not supported between instances of 'str' and 'int' [duplicate] - python

I encountered the error
'>' not supported between instances of 'str' and 'int'
while trying to print the below lines in Pandas dataframe
print (survey_df_clean.shape)
print (survey_df_clean[survey_df_clean['text']>30].shape)
Should I try to convert them to int and how would that work in this statement?

First make sure that all value of survey_df_clean['text'] is the same, if you want to convert as numeric, do this :
survey_df_clean['text'] = pd.to_numeric(survey_df_clean['text'])
Then do this
survey_df_clean.loc[survey_df_clean['text']>30].shape

This message suggests, that you try to compare a string object (str) with an integer (int).
The expression
survey_df_clean['text']
will probably return a string. Therefore, you cannot directly compare it with the number 30. If you want to compare the length of the entry, you can use the pandas.Series.str.len() operation as you can see here.
If this field should actuallty contain an integer, you can use this method (pandas.to_numeric) to cast it from str to int.

survey_df_clean['text'] might have NAN or str values in it some where.
to find out :
survey_df_clean['text'].isnull().sum()
if they are,first take care of them then apply
print (survey_df_clean[survey_df_clean['text']>30].shape)

I had the same error message when trying to use that conditional. What intrigued me was that the same command had run correctly on another notebook.
The difference was in how I read the csv file. This was the troublesome one:
df=pd.read_csv('data.csv')
And when I put the decimal argument it worked:
df=pd.read_csv('data.csv', decimal=',')
Obviously, it'll depend on how your data is organized. ;)

This is because values in 'text' column are of type str and you are comparing str with int.
You can do a quick check for getting type of 'text' column.
print(type(survey_df_clean['text'][:1][0]))
For comparing you can do as following
survey_df_clean[survey_df_clean['text'].astype(int)>30]

Related

is there a way to use numbers in a python array with strings

I get this error from python when I try to run my program does anyone know how to fix it.
ops.append(i+".)"+names[i]+"'s Living Quarters\n")
TypeError: unsupported operand type(s) for +: 'int' and 'str'
ops is a array for choices.
names is a array with names to be made in to the ops array with a number for printing.
i is a increasing number for the choice number.
sorry if there have been other questions like this, I couldn't find a solution
You'll need to convert your integer to a string before you can concatenate it. You can do this with str(i).
Or you can accomplish your append line with f-strings, like so:
ops.append(f"{i}.) {names[i]}'s Living Quarters\n")
ops.append(str(i)+".)"+str(names[i])+"'s Living Quarters\n")
Should work!
str(VARIABLE) converts the VARIABLE into STR(String)
You can use an integer in a string by either converting the integer to a string using
str(variable), or by formatting it in the string using F-strings.
String formatting example:
stringName = f"Number: {integer_variable}"
Which can also be used for other variable types and is a bit more readable than concatenating a ton of variables to strings using +
There's lots of fun ways to format strings in Python. I tend to prefer string.format just because of the flexibility.
ops = "{}{}.) {}'s Living Quarters\n".format(ops, i, names[i])
Ideally, you'd include the formatting for ops in there as well, but since I didn't have the code you used to generate it , I just showed you the closest I could.

How to convert timestamp into integer?

I've got a value that is of the type:
'pandas._libs.tslibs.timestamps.Timestamp'
but I want to convert it into:
'int'
Simply using int() doesn't work and gives me the following error message:
int() argument must be a string, a bytes-like object or a number, not 'Timestamp'
Edit: to be clear, I only want the type to change to integral. However I want the number to stay the same.
You should use the class (pandas._libs.tslibs.timestamps.Timestamp) methods. pandas._libs.tslibs.timestamps.Timestamp.timestamp() returns the timestamp, however it is a float instead of an int.

Python: not all arguments converted during string format

I have a panel for 5 years. Each person [aa_cod_fiscm] declares his income [cc_red_lrd] each year. I am trying to have a difference of declaration between each year and the previous one [difprev]. My code is
data["difprev"]= data.groupby(data.aa_cod_fiscm % 5).cc_red_lrd.diff()
All the variables are integers, but I get the following error
TypeError: not all arguments converted during string formatting
I don't know why. Can you help me , please?
I think your issue is that the % is being evaluated as a formatter rather than a mod function because data.aa_cod_fiscm is evaluating as a str object. Maybe this is how it is stored in that attribute? You could try casting it to an int.
try this:
data["difprev"]= data.groupby(int(data.aa_cod_fiscm) % 5).cc_red_lrd.diff()

Why can I not convert an object to int in Python and how do I check the troublesome data?

I am trying to run the code:
df["columnname"].astype(int)
And it does not convert my datatype to int. Instead, it's still listed as an object. There are a lot of rows in the column, but I quickly did a sort in Excel and they were all numbers. Integers in fact. Why does Python think there's a string in there, when there is not. I've tried float as well and stubbornly (just to make sure there's not a non-int in there) and it still thinks it's a string.
Assuming Excel is wrong, how do I check exactly which value cannot be converted to an int, and is causing the problem.
You may need to explicity tell python to change the datatype as follows:
df["columnname"] = df["columnname"].astype(int)

Appending a 0 in front of a variable in Python

I have a variable called fab, and a Django queryset. As follows,
fab = self.request.GET.get('fab')
and my queryset,
queryset_df = Table1.objects.filter(Q(fab=int(fab)) | Q(fab=int(0+fab))).values_list('masks').distinct()
As seen I want to append a zero in front of the fab in my Q, because sometimes the fab comes as a integer value and sometimes with a 0 infront. When I tried to add by +, it returned me an error like, unsupported operand type(s) for +: 'int' and 'unicode'. Any idea why? Thanks in advance.
You should cast the 0 to unicode or string before concatenating.
fab = "0" + fab
You can not concatenate a unicode variable with an integer, you can also not concatenate strings with integers or floats, so you need to convert one to the correct type.
It sounds like your logic is wrong somewhere,
You are either trying to force an integer into a CharField or you are trying to force a string into an IntegerField. If its the former you're always going to struggle with having to cast values to get the correct results and if its the latter the leading 0 is pointless.
You should try to use the correct field type, it will even help with your model's validation.

Categories