Back to Basics: Executing different commands using Command Line and Spyder
It has been 2 years since I last laid my hands on coding.
My last memory of coding or programming was crawling my way through graduation by finishing my thesis on comparing several machine learning methods and creating a sort of new one by combining these methods in order to get an analysis of the solar panel parameters which can be used in improving its efficiency.
Even before that grueling experience, I also had several years in my undergraduate degree coding several projects using C, SQL, HTML, Matlab, MPLab, etc. I finished all of these courses albeit not with flying colors compared with my other batchmates. I even had to repeat one course 3x or I think 4x including the first time I dropped it haha (hello Coe 115 Intoduction to Embedded Microcontrollers). It was really frustrating if the code is not running or you can’t let your code do what you want, but on the rare times that you finally found the bugs or your code miraculously worked, it was pure bliss.
Looking back, I still remember some stuff, but most of it are gone in my head since I didn’t practice it regularly for a couple of years now.
Learning data science, I knew I had to face these programming languages once again, or even had to learn new ones. Throughout my experiences, I believe the knowledge and skill of a person depends mostly on his/her foundation of the basics, so I acknowledge that learning the foundation and the basics of data science and executing basic commands on several platforms will be one of the keys to move forward onto the harder concepts in this field. As most people say, don’t be afraid to be a beginner again.
Now, while studying the basics, I’ll lead you through what I’ve learned on some basic commands and techniques in working around the Command Prompt and Spyder environment.
I. Introduction to the Command Prompt
Most of the time, we will need to use the command line to use some specific tools and techniques which cannot be done through the Windows Graphical User Interface (GUI). In most cases, working on the command line is also faster than using the GUI.
- ACCESSING COMMAND PROMPT. On the search bar of your Start Menu, type cmd and click the Command Prompt to launch it
2. COMMAND LINE WINDOW. You will see the Windows Command Line and initially, it will show your current working directory or where you are in your computer
3. MOVE INTO ANOTHER DIRECTORY. To move around in another directory, just type cd (which means change directory) followed by the directory name of where you want to go to
- cd Documents
Note: You can only navigate through directories starting with your current directory, so it must be ensured that the next directory you will go into is included in the list of directories of your current working directory. You can see the list of directories available by entering dir in the command line
- dir
The list of the files and folders will be displayed with the details about each of them like the size and date and time it was modified.
4. GOING ONE FOLDER UP. If you want to go back to your previous directory, just enter
- cd ..
5. MOVE TO A DIRECTORY WITHIN A DIRECTORY. You can also move directly to different directories or files without having to change directories one step at a time. You only need to right the entire path towards the specific directory you want to access. For example, you want to access FTW in Documents,
In the command line, enter
- cd Documents\FTW
to go directly to the desired directory in one step. Note on the use of \ (backslash) when writing paths to directories.
6. GOING MULTIPLE DIRECTORIES BACK. If you want to go multiple steps back to your initial directory, you may do this by entering
- cd ../..
7. CREATING DIRECTORIES. Type mkdir or md followed by the name of the new folder to create a new folder in your current directory. You may be able to check the created folder by using dir
You may also create a folder with subfolders by typing mkdir followed by the name of the folder, a backslash ‘\’ and name of subfolder
- mkdir medium\blog
8. RENAMING FOLDERS AND FILES. You can use the ren command in renaming folders and files. Just type ren followed by the folder to be renamed and the new folder name
- ren hw Tableau
When you want to rename the file, type ren and write the whole name of the file including the extension
- ren “9 tab 1.PNG” 9tab1.PNG
If a file or folder has spaces on the name, you need to enclose it in a double apostrophe “ ” for the command prompt to recognize it as a folder/file with spaces.
9. COPY. Copying within the directory will only need you to type copy followed by the file to be copied and the new name and extension of that file
- copy 9tab1.PNG 9tab1.JPEG
If you want to copy a file into a different location, you need to include the entire path where the file is found and where it is to be copied.
For copying folders, you need to use xcopy /s /i followed by the path on where it will be copied. The /s parameter ensures that all subfolders will be copied except the empty ones and the /i parameter will copy all the files in a new created directory if the folder does not exist.
10. DELETE. You can use the del command followed by the file name to delete files while the rd command followed by the folder name to delete folders.
Bonus: Some shortcuts in working around the command line.
A. TAB. When you want to change directory and is too lazy to type the whole directory name, just type cd followed by the first letter of the directory and hit Tab on your keyboard. Hitting Tab will automatically fill-in the command line with the directory that starts with the letter you typed.
For example, you have Desktop, Documents and Downloads in your directory which all start with the letter ‘D’. Enter cd D and hit Tab
Hitting Tab will automatically fill-in names of directory in Alphabetical order
Hitting Tab once again will lead you through the next directory
And another hit on tab will direct you to another directory
B. ARROW KEYS. You can also use the up and down arrow keys on your keyboard to access the previous commands you used.
C. CLEAR. Once the command line window is full and you want to clear the commands you used, just enter cls to clear the command line window
D. TOP OF DIRECTORY TREE. To navigate to the top of the directory tree, tye cd \ and hit enter. This will take you to the root of the “C:” drive
E. HELP. Lastly, don’t be afraid to ask for help. Just type help in the command line and press enter. A list of available commands will be shown including a short description of the command.
You may also type help and the specific command to display more information about this command.
- help cd
II. Python Basics in Spyder
To access Spyder, just type Spyder on your Windows search bar and click the Spyder app
The Spyder working environment will appear. The default environment will show a code block where you will write your codes, a path name of the working directory you’re in, a window to see the variable or file explorer and the Python console that shows the result of your code once you run it.
To print a simple text, use the print command.
- print(“Welcome to Python Basics”)
To run the code, you can click the green play button on the toolbar, or hit F5 or press CTRL+ENTER
You may assign values to several variables and perform mathematical expressions to get a certain outcome. Once the code is ran, the variable explorer tab will show the changes in the values of the variables you created.
- x = 23
- y = 45
- z = x+y
Import the Python library pandas and name it pd by using import pandas as pd. Once pandas as imported, you can now read a csv file that is located in the current working directory you’re in by using .read_csv() function that will automatically parse the file as a pandas DataFrame. If the CSV file is not in the same folder with your directory, you need to specify its file path.
- import pandas as pd
- df=pd.read_csv(‘survey_results_public.csv’)
Once you run the code, the variable explorer will be updated and will include your new DataFrame df. You can click the variable name df and the dataset will appear. The public dataset I chose is the one from Stack Overflow which shows the results of their Annual Developer Survey. You may find the dataset here.
The size of the dataset can be shown in the variable explorer tab but you may also see this using the command
- df.shape
which shows the number of rows and columns respectively. To count unique entries in a column, you can use the value_counts() function. You will see how many responded that they are hobbyist developers.
Lastly, you may also represent a percentage of the total number by passing an argument in the value_counts() function. Here, a percentage per developer status is shown, whether they are already developers or students or hobbyists, etc.
These are just the basic and cover only a really small percentage of the whole command line, spyder and python knowledge and skills but as I’ve said, basics or foundations are very important to establish your experience throughout this whole data science journey.
There’s still a lot to learn and more obstacles to face but let’s just keep on keeping on! #koko