Pandas Data Structure
DATA STRUCTURES IN PANDAS:
One of the most important things in Pandas is Data Structure.
Pandas have three data structures:
- Series
- DataFrame
- Panel
SERIES:
- Series is a one-dimensional array.
- It holds any type of data.
WAYS TO CREATE A SERIES:
- Series using list
- Series using array
- Series using dictionary
SERIES USING LIST:
Creating a series with the list values.
Example:
import pandas as pd
list_data = ['pandas', 'numpy', 'pytest', 'pywin32']
result = pd.Series(list_data)
print(result)
list_data = ['pandas', 'numpy', 'pytest', 'pywin32']
result = pd.Series(list_data)
print(result)
SERIES USING ARRAY:
Creating a series with the array values.
Example:
import pandas as pd
import numpy as np
array_data = np.array(['pandas', 'numpy', 'pytest', 'pywin32'])
result = pd.Series(array_data)
print(result)
import numpy as np
array_data = np.array(['pandas', 'numpy', 'pytest', 'pywin32'])
result = pd.Series(array_data)
print(result)
SERIES USING DICTIONARY:
Creating a series with the dictionary data.
Example:
import pandas as pd
dict_data = { 'Name': 'Vetri', 'age': 20}
result = pd.Series(dict_data)
print(result)
dict_data = { 'Name': 'Vetri', 'age': 20}
result = pd.Series(dict_data)
print(result)
CREATE LABELS:
Change the index values with the labels specified.
Example:
import pandas as pd
list_data = ['pandas', 'numpy', 'pytest', 'pywin32']
result = pd.Series(list_data, index=['a', 'b', 'c', 'd'])
print(result)
list_data = ['pandas', 'numpy', 'pytest', 'pywin32']
result = pd.Series(list_data, index=['a', 'b', 'c', 'd'])
print(result)
Able to access the particular values based on the label.
print(result["a"])
If the length of the index doesn't match the length of the value, then it throws the error.
Thanks for choosing our blog 😊.
Comments
Post a Comment