Working with Data in Python
Working with Data in Python
Copied!
Syntax:
1. 1
2. 2
Copied!
Syntax:
1. 1
Copied!
Syntax:
1. 1
2. 2
Copied!
with open() Opens a file Syntax:
using a with
1. 1
block,
ensuring 1. with open(filename, mode) as file: # Code that uses the file
automatic
file closure Copied!
after usage.
Example:
1. 1
2. 2
Copied!
Pandas
Package/Method Description Syntax and Code Example
Syntax: dataframe_name = pd.read_csv("filename.csv") Example: df =
.read_csv() Reads data from a `.CSV` file and creates a DataFrame.
pd.read_csv("data.csv")
Syntax:
1. 1
1. dataframe_name = pd.read_excel("filename.xlsx")
Copied!
.read_excel() Reads data from an Excel file and creates a DataFrame.
Example:
1. 1
1. df = pd.read_excel("data.xlsx")
Copied!
Syntax:
1. 1
1. dataframe_name.to_csv("output.csv", index=False)
Copied!
.to_csv() Writes DataFrame to a CSV file.
Example:
1. 1
1. df.to_csv("output.csv", index=False)
Copied!
Syntax:
1. 1
2. 2
Copied!
Access Columns Accesses a specific column using [] in the DataFrame.
Example:
1. 1
2. 2
1. df["age"]
2. df[["name", "age"]]
Copied!
Syntax:
1. 1
1. dataframe_name.describe()
Copied!
describe() Generates statistics summary of numeric columns in the DataFrame.
Example:
1. 1
1. df.describe()
Copied!
Syntax:
1. 1
2. 2
Copied!
Removes specified rows or columns from the DataFrame. axis=1
drop()
indicates columns. axis=0 indicates rows. Example:
1. 1
2. 2
Copied!
dropna() Removes rows with missing NaN values from the DataFrame. Syntax:
axis=0 indicates rows.
1. 1
1. dataframe_name.dropna(axis=0, inplace=True)
Copied!
Example:
1. 1
1. df.dropna(axis=0, inplace=True)
Copied!
Syntax:
1. 1
1. dataframe_name.duplicated()
Copied!
duplicated() Duplicate or repetitive values or records within a data set.
Example:
1. 1
1. duplicate_rows = df[df.duplicated()]
Copied!
Syntax:
1. 1
1. filtered_df = dataframe_name[(Conditional_statements)]
Copied!
Filter Rows Creates a new DataFrame with rows that meet specified conditions.
Example:
1. 1
Copied!
Syntax:
1. 1
2. 2
Copied!
Syntax:
1. 1
1. dataframe_name.head(n)
Copied!
head() Displays the first n rows of the DataFrame.
Example:
1. 1
1. df.head(5)
Copied!
Syntax:
1. 1
1. import pandas as pd
Copied!
Import pandas Imports the Pandas library with the alias pd.
Example:
1. 1
1. import pandas as pd
Copied!
Syntax:
1. 1
1. dataframe_name.info()
Copied!
Provides information about the DataFrame, including data types and
info()
memory usage. Example:
1. 1
1. df.info()
Copied!
merge() Merges two DataFrames based on multiple common columns. Syntax:
1. 1
Copied!
Example:
1. 1
Copied!
Syntax:
1. 1
Copied!
1. print(df)
2. df
Copied!
Syntax:
1. 1
Copied!
replace() Replaces specific values in a column with new values.
Example:
1. 1
Copied!
Syntax:
1. 1
1. dataframe_name.tail(n)
Copied!
tail() Displays the last n rows of the DataFrame.
Example:
1. 1
1. df.tail(5)
Copied!
Numpy
Package/Method Description Syntax and Code Example
Syntax:
1. 1
1. import numpy as np
Copied!
Importing NumPy Imports the NumPy library.
Example:
1. 1
1. import numpy as np
Copied!
Syntax:
1. 1
2. 2
Copied!
np.array() Creates a one or multi-dimensional array,
Example:
1. 1
2. 2
Copied!
Example:
1. 1
2. 2
3. 3
- Calculates the mean of array elements 4. 4
- Calculates the sum of array elements 5. 5
Numpy Array Attributes - Finds the minimum value in the array
- Finds the maximum value in the array 1. np.mean(array)
2. np.sum(array)
- Computes dot product of two arrays 3. np.min(array
4. np.max(array)
5. np.dot(array_1, array_2)
Copied!
© IBM Corporation. All rights reserved.