Python Python Python Notes: 100 Commands, Tips & Tricks for Data Analysis

Python Notes: 100 Commands, Tips & Tricks for Data Analysis

AS
Gladson
| May 9, 2026 |
read
#python #data-analysis #pandas #numpy #commands #tips-and-tricks

Python Notes: 100 Commands, Tips & Tricks for Data Analysis

Python is a versatile language, and its power in data analysis is unmatched. Here are 100 commands, tips, and tricks to help you master data analysis with Python.


1. Basic Python Commands

  1. print(): Output data to the console.
  2. type(): Check the data type of a variable.
  3. len(): Get the length of a string, list, or dictionary.
  4. input(): Get user input.
  5. int(), float(), str(): Convert between data types.

2. Lists and Dictionaries

  1. list.append(): Add an item to the end of a list.
  2. list.extend(): Add multiple items to a list.
  3. list.pop(): Remove and return the last item.
  4. dict.keys(): Get all keys in a dictionary.
  5. dict.values(): Get all values in a dictionary.
  6. dict.items(): Get all key-value pairs.

3. NumPy Essentials

  1. import numpy as np: Import the NumPy library.
  2. np.array(): Create a NumPy array.
  3. np.zeros(): Create an array of zeros.
  4. np.ones(): Create an array of ones.
  5. np.arange(): Create an array with a range of values.
  6. np.linspace(): Create evenly spaced values over a range.
  7. np.reshape(): Change the shape of an array.
  8. np.mean(): Calculate the mean of an array.
  9. np.median(): Calculate the median.
  10. np.std(): Calculate the standard deviation.

4. Pandas for Data Manipulation

  1. import pandas as pd: Import the Pandas library.
  2. pd.read_csv(): Load a CSV file into a DataFrame.
  3. df.head(): View the first 5 rows of a DataFrame.
  4. df.tail(): View the last 5 rows.
  5. df.info(): Summary of the DataFrame.
  6. df.describe(): Statistical summary of numerical columns.
  7. df.shape: Get the number of rows and columns.
  8. df.columns: Get the column names.
  9. df.dtypes: Get the data types of columns.
  10. df['column_name']: Select a single column.
  11. df[['col1', 'col2']]: Select multiple columns.
  12. df.iloc[]: Select rows and columns by index.
  13. df.loc[]: Select rows and columns by label.
  14. df.drop(): Remove rows or columns.
  15. df.rename(): Rename columns.
  16. df.sort_values(): Sort the DataFrame by a column.
  17. df.groupby(): Group data by a column.
  18. df.merge(): Combine two DataFrames (SQL-like join).
  19. df.concat(): Concatenate DataFrames.
  20. df.isnull(): Check for missing values.
  21. df.fillna(): Fill missing values.
  22. df.dropna(): Remove rows with missing values.
  23. df.duplicated(): Check for duplicate rows.
  24. df.drop_duplicates(): Remove duplicate rows.
  25. df.apply(): Apply a function to rows or columns.
  26. df.value_counts(): Count unique values in a column.
  27. df.unique(): Get unique values in a column.
  28. df.pivot_table(): Create a pivot table.
  29. df.sample(): Get a random sample of rows.

5. Data Visualization (Matplotlib & Seaborn)

  1. import matplotlib.pyplot as plt: Import Matplotlib.
  2. plt.plot(): Create a line plot.
  3. plt.scatter(): Create a scatter plot.
  4. plt.bar(): Create a bar chart.
  5. plt.hist(): Create a histogram.
  6. plt.xlabel(), plt.ylabel(): Set axis labels.
  7. plt.title(): Set the plot title.
  8. plt.show(): Display the plot.
  9. import seaborn as sns: Import Seaborn.
  10. sns.boxplot(): Create a box plot.
  11. sns.heatmap(): Create a heatmap.
  12. sns.pairplot(): Plot pairwise relationships.

6. Advanced Tips & Tricks

  1. List Comprehension: [x**2 for x in range(10)].
  2. Lambda Functions: sum = lambda x, y: x + y.
  3. Map Function: list(map(str.upper, ['a', 'b'])).
  4. Filter Function: list(filter(lambda x: x > 5, [1, 6, 2, 8])).
  5. Zip Function: Combine two lists: list(zip(names, scores)).
  6. Enumerate: Get index and value: for i, v in enumerate(list):.
  7. F-strings: f"Value: {val}" for easy formatting.
  8. Handling Large Files: Use chunksize in pd.read_csv().
  9. Setting with Copy Warning: Use .copy() to avoid it.
  10. Optimize Memory: Convert columns to category dtype.
  11. Query Method: df.query('age > 25').
  12. String Methods: df['name'].str.lower().
  13. Datetime Conversion: pd.to_datetime(df['date']).
  14. Extract Date Parts: df['date'].dt.year.
  15. Rolling Windows: df['val'].rolling(window=7).mean().
  16. Shift Function: df['val'].shift(1) for lag analysis.
  17. Correlation Matrix: df.corr().
  18. Save to CSV: df.to_csv('output.csv', index=False).
  19. Save to Excel: df.to_excel('output.xlsx').
  20. Using .at and .iat: Faster than .loc for single values.
  21. Explode: Turn list-like columns into rows: df.explode('col').
  22. Crosstab: pd.crosstab(df['A'], df['B']).
  23. Pipe Method: Chain operations: df.pipe(func1).pipe(func2).
  24. Isin: Filter by multiple values: df[df['A'].isin([1, 2, 3])].
  25. Where Method: df['A'].where(df['A'] > 0, 0).
  26. Cut and Qcut: Bin data: pd.cut(df['age'], bins=3).
  27. Get Dummies: One-hot encoding: pd.get_dummies(df['col']).
  28. Melt: Unpivot a DataFrame: pd.melt(df).
  29. Stack and Unstack: Reshape by index levels.
  30. Lookup with Map: df['cat'] = df['id'].map(mapping_dict).
  31. Chain Methods: df.dropna().groupby('A').mean().
  32. Progress Bar: Use tqdm for long loops.
  33. Profile Your Data: Use pandas_profiling (now ydata-profiling).
  34. Interactive Plots: Use plotly for interactive charts.
  35. Style DataFrames: df.style.background_gradient().
  36. Read from SQL: pd.read_sql(query, engine).
  37. Read from Clipboard: pd.read_clipboard().
  38. Help Command: help(pd.DataFrame) to see documentation.

Conclusion

These 100 commands are just the tip of the iceberg. Python’s ecosystem is vast, and continuous practice is key to becoming a pro at data analysis. Happy coding!