Pandas contain 2 important data structures.
- Series
- dataFrame
1.Series:
Series is one dimensional , labeled array, capable of storing any type of data (integers, strings, floating point numbers, Python objects, etc.)
import pandas as pd
my_data=["one","dimension","array",1,"D",["array"]] #list
my_series=pd.Series(my_data) #converting list to series
variable_name=pd.Series(data) is the syntax to create a series.
my_series=pd.Series(my_data)
.Series(my_data) also takes index as a parameter.
If index parameter is not used, the index positions of data are used as label.
The passed index is a list of axis labels.
Here, data can be many different things:
- List
a Python dict
an ndarray
a scalar value (like 5)
1)Series from list:
2)Series from dictionary:
my_dic={"name":"priya","id":1001,"dept":"ECE"}
series=pd.Series(my_dic)
print(series)
Index, if given while working with dictionary series, will try to pull the value from the dictionary. If the element in index is not present in dictionary, it returns NaN(not a number)
3)Series from ndarray:
my_series=pd.Series(np.random.rand(5), index=["a","b","c","d","e"])
np is numpy library, random.rand(5) generates five random value.
4)Series from scalar value:
Series can also be creating using scalar value.
my_series=pd.Series(5,index=["a","b","c"])
If the data is scalar, then index must be provided.
The data will be repeated to match the index.
No comments:
Post a Comment