Home > OS >  How to locate columns in data frame with index?
How to locate columns in data frame with index?

Time:03-27

    Asset   Liab    Net Worth
Date
1/1 8.99 K  -19.65 K    -10.66 K
1/2 8.99 K  -19.66 K    -10.67 K

The data looked something like that.

Below is what I want to achieve

> df['Asset'] = df['Asset'].str.rstrip('K')
> df['Liab'] = df['Liab'].str.rstrip('K')
> df['Net Worth'] = df['Net Worth'].str.rstrip('K')

I want to make a loop for it to process every column, but

> df.columns[] #only return the column's name not the whole list
> df.iloc[] #return the value based on vertical index
> df.loc[] #shows invalid syntax

I ended up doing this

> def removeSuffix(df, suffix):
    df = df.T
    i = 0
    while i < len(df): 
        df.iloc[i] = df.iloc[i].str.rstrip(suffix)
        i  = 1
    return df

One wired thing, this function works in VScode's interactive window, but shows syntax error in terminal.

Sorry if this question is dumb, I'm new to this. I'm so clueless on how to get the entire column.

CodePudding user response:

Use apply to apply the function to each column

df = df.apply(lambda col: col.str.rstrip('K'))

Note that the values after stripping are still strings. If you want them as floats you can do

df = df.apply(lambda col: col.str.rstrip('K').astype(float))
  • Related