To take the first x columns of a DataFrame in Python using pandas, you can use the indexing operator []
and specify the range of columns you want to keep.
Here's an example:
main.py247 chars12 lines
This will output the DataFrame with only the first x
columns:
main.py32 chars5 lines
Note that the indexing in pandas starts from 0. So, iloc[:, :x]
selects all rows (:
) and the first x
columns (:x
) in the DataFrame df
.
gistlibby LogSnag