Python: Pandas Lesson 4
Learning : Panda Lesson 4
Subject: DataFrame Columns: Hide, Drop, rename
We still workinng on dataframe and columns, we will go thrght some function and at the end I will just add a line to save the dataframe in a new CSV file. So let’s start.
We still working on our data_file_zoo.csv and here i am copying the column we have in the file or in our df.
print(‘\n\n Columns in thedataFrame..\n’,df.columns)
![]() |
Now we have a list of columns in our DataFrame, some time we want to hide a column, here we will creat a variable and whenever we call this variable the column will not be shone on the screen.
Hide column ‘supervisor’
In this line we will set a variable to hide supervisor column, and just for sceen-shop we will present 6 random rows
hide_supervisor=df.drop([‘supervisor’], axis=1)
print(‘\n\n Sample data after hiding supervisor column\n’,hide_supervisors.sample(6))
![]() |
In the upper case, we may have a password column or some key information column that we don’t want to be shown in the dataframe, then it’s good idea to create a DataFrame without this column an use it.
If we have a dataframe and we are examining some thing and don’t want to show all columns every time we print the df, so just show (say three) columns. To do this, first we will print the columns names so we know what we have in the df, then using coming code we will select whatever we want to show.
animal_cage_years=df[[‘animal’,’cage_no’,’years’]]
print(‘\n\n Show selected Columns from df\n’,animal_cage_years.sample(6))
![]() |
Now we will drop a column from the df, I will select ‘supervisor’, just like this:
print(‘\n\n Drop column ”supervisor” form the df’)
print(df.drop([‘supervisor’],axis=1))
![]() |
To be Aware: In the above case, if we use the command on df and we add inplace=True then this will change the df, so any time we calling the df it will be without the ‘supervisor’ column. Here is the code..
df.drop([‘supervisor’], inplace=True, axis=1)
print(‘\n\n’,df)
If we want to hide more than one columns we just add them in the command like this:
hide_years_cages=df.drop([‘years’,’cage_no’], axis=1)
print(hide_years_cages.sample(6))
![]() |
If we want to check wither or not a df contain column c_name if yes hide-it else print ‘Column not found’.
If column ‘cage_no’ in df hide it.
if ‘cage_no’ in df.columns:
hide_cage = df.drop([‘cage_no’], axis=1)
print(‘\n\n’,hide_cage.sample(6))
else:
print(‘Column not found’)
and we can in the else block just showing another dataframe.
![]() |
:: Pandas Lessons Post ::
Lesson 1 | Lesson 2 | Lesson 3 | Lesson 4 |
Lesson 5 |
-
July 29, 2019 at 8:24 amPython: Pandas Lesson 5 | Ali's Photography Space...
-
July 29, 2019 at 8:28 amPython: Pandas Lesson | Ali's Photography Space...
-
July 29, 2019 at 8:28 amPython: Pandas Lesson 3 | Ali's Photography Space...
-
July 29, 2019 at 8:29 amPython: Pandas Lessons | Ali's Photography Space...
-
July 31, 2019 at 8:46 amPython: Pandas more commands | Ali's Photography Space...
-
August 4, 2019 at 8:32 amPython: Pandas Lesson 6 | Ali's Photography Space...
-
August 6, 2019 at 8:30 amPython: Pandas Lesson 7 | Ali's Photography Space...
-
August 8, 2019 at 8:32 amPython: Pandas Lesson 8 | Ali's Photography Space...
-
August 11, 2019 at 5:35 amPython: Pandas Lesson 9 | Ali's Photography Space...
-
August 13, 2019 at 8:36 amPython: Pandas Lesson 10 | Ali's Photography Space...
-
August 15, 2019 at 8:33 amPython: Pandas Lesson 11 | Ali's Photography Space...
-
August 18, 2019 at 8:40 amPython: Pandas Lesson 12 | Ali's Photography Space...
-
August 25, 2019 at 9:22 amlearning – Temp | Ali's Photography Space...