0

When I apply f"" string on my text data it procduces the following error. AttributeError: 'str' object has no attribute 'str'.

The simple code is below, I am providing just one line so that it will save your time. I just want to apply f"" in this way. I know the problem but don't know how to figure it. Thanks

caption = f"{caption.str.lower().str.rstrip('.')}"

albert
  • 158
  • 1
  • 10
  • if you're asking about pandas string operations, you should include the `pandas` tag, not `tensorflow` or `keras`. also, to get the best help, please always include the code you're working with, an example of your data (at the very least, the types that you're working with), and ideally a full [mre]. See also this guide to creating [minimal examples in pandas](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). check out the [ask] guide for more info. – Michael Delgado Jul 03 '22 at 20:34

1 Answers1

0

You may be familiar with the pandas library's .str accessor which make pandas ports of python string methods available to a Series. But if you're working with python builtin str types, you don't need the .str accessor. Simply call:

caption = f"{caption.lower().rstrip('.')}"

See the python builtin types docs on string methods for more info.

Michael Delgado
  • 13,789
  • 3
  • 29
  • 54
  • Thanks it helped me, How can I split this string line by line, I mean how to make this code `caption = f"{caption.lower().rstrip('.')}"` to split my string line by line. – albert Jul 03 '22 at 18:28
  • This `caption` is panda dataframe, if there is any solution to split this `caption` dataframe by rows. – albert Jul 03 '22 at 18:38
  • so - if it's a pandas dataframe then you *should* use .str methods, but don't try to include it in a format string group. just use `caption.str.lower().str.rstrip(".")`. no need to put the series into a `f"{}"`, which would force the series into a python string. – Michael Delgado Jul 03 '22 at 20:28
  • if you're running into the error in your question, you may have converted your dataframe/series into a python string in an earlier step. So you may need to go back and make sure your `caption` object stays as a pandas object. Generally, don't force your pandas object to go through python string formatting e.g. with `f"{pandas_obj}"` or `"{}".format(pandas_obj)` unless you know you want to turn the pandas object into a single python string. – Michael Delgado Jul 03 '22 at 20:36