Home > Blockchain >  method chaining does not perform all actions
method chaining does not perform all actions

Time:01-29

A cleaner way to write this code?

Unnamed: 0  name      AP          VP   kP
0             a     14.0       -0.96   0    
1             b     49.0        0.61   0
2             c     44.0         nan   0
3             d      6.0       -0.27   0    
# Cleaning extras, round off, nan 
df.rename(columns={'Unnamed: 0':'i'}, inplace=True)
df = df[['name','i', 'AP','VP' ]]
df.rename(columns={'AP':'a%','v%'}, inplace=True)

cols = df.columns.drop('name')
df[cols] = df[cols].apply(pd.to_numeric, errors='coerce')
df[cols] = df[cols].round(2)

df.fillna(0, inplace=True)
df = df[df.name != 'name']

df.head()

I get these warnings 3 times for the above,

A value is trying to be set on a copy of a slice from a DataFrame
Try using .loc[row_indexer,col_indexer] = value instead

I have tried different combinations but some actions are not implemented. Recommendations for improving the code?

CodePudding user response:

I am not clear on what you are trying to do but you can method chain like this.

Make df

name = ['jon', 'joe']
i = [1, 2]
ap = [3, 4]
vp = [2, 3]

mydict = {'name':name, 'Unnamed: 0':i, 'AP':ap, 'VP':vp}
df = pd.DataFrame(mydict)

the first block

Use filter to select the columns you want and rename them.

df.filter(['name','Unnamed: 0', 'AP','VP']).rename(columns={'Unnamed: 0':'i', 'AP':'a%'})

output

    name    i   a%  VP
0   jon     1   3   2
1   joe     2   4   3

the second block

You might consider using astype rather than to_numeric which is called on pandas.

df.drop(columns='name').astype('int32').round(2)

output

        Unnamed: 0  AP  VP
0       1           3   2
1       2           4   3
  •  Tags:  
  • Related