I/O Pandas Series and Dataframe
Creating Pandas Series.
# Create series with Pandas
series = pd.Series(data = ['Geeks','for','geeks'],
index = ['A','B','C'])
series
Output:
A Geeks
B for
C geeks
dtype: object
Create Pandas Dataframe
Creating Pandas Dataframe.
data = {'Fruits': ['Mango', 'Apple', 'Banana', 'Orange'],
'Quantity': [40, 20, 25, 10],
'Price': [80, 100, 50, 70]
}
# Create Pandas Dataframe with dictionary
df = pd.DataFrame(data)
print(df)
Output:
Fruits Quantity Price
0 Mango 40 80
1 Apple 20 100
2 Banana 25 50
3 Orange 10 70
Check the Data Types
We will check data types with the help of dtypes() function.
# check Data types
df.dtypes
Output:
Fruits object
Quantity int64
Price int64
dtype: object
Check the dataframe shape
We will check data types with the help of shape() function.
# check the shape of dataset
df.shape
Output:
(4, 3)
Check the data info
df.info() methods return the all information of your dataset.
# check info
df.info()
Output:
<class 'pandas.core.frame.DataFrame'>
Index: 4 entries, a to d
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Fruits 4 non-null object
1 Quantity 4 non-null int64
2 Price 4 non-null int64
dtypes: int64(2), object(1)
memory usage: 128.0+ bytes
Change the Data type
df['Quantity'] = df['Quantity'].astype('int32')
df['Fruits'] = df['Fruits'].astype('str')
df['Price'] = df['Price'].astype('float')
df.info()
Output:
<class 'pandas.core.frame.DataFrame'>
Index: 4 entries, a to d
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Fruits 4 non-null object
1 Quantity 4 non-null int32
2 Price 4 non-null float64
dtypes: float64(1), int32(1), object(1)
memory usage: 112.0+ bytes
Print data frame values as NumPy array
df.values
Output:
array([['Mango', 40, 80],
['Apple', 20, 100],
['Banana', 25, 50],
['Orange', 10, 70]], dtype=object)
Pandas Cheat Sheet for Data Science in Python
Pandas is a powerful and versatile library that allows you to work with data in Python. It offers a range of features and functions that make data analysis fast, easy, and efficient. Whether you are a data scientist, analyst, or engineer, Pandas can help you handle large datasets, perform complex operations, and visualize your results.
This Pandas Cheat Sheet is designed to help you master the basics of Pandas and boost your data skills. It covers the most common and useful commands and methods that you need to know when working with data in Python. You will learn how to create, manipulate, and explore data frames, how to apply various functions and calculations, how to deal with missing values and duplicates, how to merge and reshape data, and much more.
If you are new to Data Science using Python and Pandas, or if you want to refresh your memory, this cheat sheet is a handy reference that you can use anytime. It will save you time and effort by providing you with clear and concise examples of how to use Pandas effectively.