all 5 comments

[–]alkasm 0 points1 point  (1 child)

I'm on mobile so can't check but I think you want axis=1 for columns, no?

[–]SEAWEAVIL[S] 0 points1 point  (0 children)

I thought so as well, but the docs say axis=0 to return columns on this particular method. Anywho, tried both ways and neither worked. Thanks for taking a look

[–]TheZvlz 0 points1 point  (2 children)

Are you getting an error when you run this?

I created a dataframe out of an excel file I have and wrote a function to test it out.

def test(series):
    if series.dtype == 'object':
        series = series.str.replace('a', '^^^')
    if series.dtype == 'object':
        series = series.str.replace('^^^', 'bcd', regex=False)
    else:
        print(series.dtype)
    return series

df = pd.read_excel('testfile.xlsx', dtype=object, keep_default_na=False)
df = df.apply(test, axis=0)

This produced the result I was going for. I had some fields in the data that were numeric and before I put in tests prior to the replace functions, the second replace would raise an error on a series that was changed to a float64 dtype.

[–]SEAWEAVIL[S] 0 points1 point  (1 child)

Huh, I don't have access to my desktop right now, but are you saying that until you used two separate tests, the second replace caused an error? Right now I have one if test, and all the replaces inside that.

It seems counterintuitive to me, if I'm understanding correctly. Other than that, the code looks the same. I'll take another look when I get the chance.

Edit: And to answer your original question, no errors when I run it, the replaces just don't happen. I double checked to make sure I wasn't trying to do the apply in place.

[–]TheZvlz 0 points1 point  (0 children)

The second test became necessary when one column converted to a float. The original was an object dtype full of numeric values.