2

I have 5 dataframes and I want to drop certain columns from them. I tried for loop, something like this -

dataframes =[af,bf,cf,df,ef,ff,gf]
for col in dataframes:
  print col.head(1)
  col = col.drop(col.columns[[0,2]],axis=1)
  print col.head(1)

I know the approach is wrong. How to do that without doing it repetitively ?

3 Answers 3

1

consider the list of dataframes dataframes

dataframes = [pd.DataFrame(dict(A=[1], B=[2], C=[3])) for _ in range(4)]

Use drop and inplace=True

for d in dataframes:
    d.drop(['B'], 1, inplace=True)

for d in dataframes:
    print(d)

   A  C
0  1  3
   A  C
0  1  3
   A  C
0  1  3
   A  C
0  1  3
Sign up to request clarification or add additional context in comments.

Comments

1

I think your solution is correct if need drop columns by positions, also is possible use list comprehension:

df = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6],
                   'C':[7,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})

df1 = df*10
dataframes = [df,df1]

#get changed list of df
dataframes = [df.drop(df.columns[[0,2]],axis=1) for df in dataframes]
print (dataframes)
[   B  D  E  F
0  4  1  5  7
1  5  3  3  4
2  6  5  6  3,     B   D   E   F
0  40  10  50  70
1  50  30  30  40
2  60  50  60  30]

#original not changed
print (df1)
    A   B   C   D   E   F
0  10  40  70  10  50  70
1  20  50  80  30  30  40
2  30  60  90  50  60  30

#get changed all df inplace, operation return `None`, so output is _
_ = [df.drop(df.columns[[0,2]],axis=1, inplace=True) for df in dataframes]
print (df1)
    B   D   E   F
0  40  10  50  70
1  50  30  30  40
2  60  50  60  30

and your solution need not assign, but inplace=True:

for col in dataframes:
  print (col.head(1))
  col.drop(col.columns[[0,2]],axis=1, inplace=True)
  print (col.head(1))

print (df1)
    B   D   E   F
0  40  10  50  70
1  50  30  30  40
2  60  50  60  30

Comments

0

To drop dataframes using column index, simply use
df.drop(df.columns[[0, 2, 5]], axis=1)

Note: Here I am passing index as 0,2, and 5. The df.columns is zero-based pd.Index

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.