Friday 10 January 2020

Choosing columns in pandas DataFrame

Sometimes, you have a lot of columns in your DataFrame and want to use only some of them.

Picking specific columns

df[‘col1]
This command picks a column and returns it as a Series
Column returned as a Series
df[[‘col1’]]
Here, I chose the column and I get a DataFrame
Column returned as a DataFrame
df[[‘col1’, ‘col2’]]
This is the same command as above — but this time, I am choosing more than one column
Two columns returned as a DataFrame

Picking certain values from a column

df[df1[‘col1’] == value]
You choose all of the values in column 1 that are equal to the value.
df[df1[‘col1’] != value]
All of the values in column 1 that are not equal to the value.
df[df1[‘col1’] < value]
All of the values in column 1 are smaller than the value.
df[df1[‘col1’] > value]
All of the values in column 1 are bigger than the value.
All of the values in ‘Police District’ are 8
df1[‘col1’] == value
Similarly to the above commands, just here you get Boolean values.
The column returned as boolean

Picking certain rows

df.ix[index]
You can actually choose a row by using its index (the number on the far left).
I chose a row with index ‘0’
df.ix[‘index name’]
This command does exactly the same thing as above but you use it when you have actually named your indices.

Deleting columns

df.drop([‘col1’,’col2'], axis=1)
This command deletes specific columns from the DataFrame
You can see that here I don’t have the two rows that I dropped
del df[‘col1’]
This command deletes a specific column from the DataFrame and modifies it — so be careful how you use it.
You can see that this column is now gone

from : https://medium.com/@kasiarachuta/choosing-columns-in-pandas-dataframe-d0677b34a6ca

No comments:

Post a Comment