Series Series are one-dimensional (1-D) arrays defined in Panda that can be used to store any data type.
Code 1: Authoring series
# Program to create series
# Import Panda Library
import pandas as pd
# Create series with Data, and Index
a = pd.Series(Data, index = Index)
Copy the code
- Scalar values can be integer values, strings
- Python dictionaries can be key, value pairs
- Ndarray
The index and value of a Series can be obtained through the two properties of the Series, index and value, respectively
obj.index
Out[5]: RangeIndex(start=0, stop=4, step=1)
obj.values
Out[7]: array([2, 9, 5, 6], dtype=int64)
Copy the code
Code 2: When data contains scalar values
# Program to Create series with scalar values
# Numeric data
Data =[1, 3, 4, 5, 6, 2, 9]
# Creating series with default index values
s = pd.Series(Data)
# predefined index values
Index =['a', 'b', 'c', 'd', 'e', 'f', 'g']
# Creating series with predefined index values
si = pd.Series(Data, Index)
Copy the code
Output:
Code 3: When data contains a dictionary
# Program to Create Dictionary series
dictionary ={'a':1, 'b':2, 'c':3, 'd':4, 'e':5}
# Creating series of Dictionary type
sd = pd.Series(dictionary)
Copy the code
Output:
Code 4: When data contains Ndarray
# Program to Create ndarray series
# Defining 2darray
Data =[[2, 3, 4], [5, 6, 7]]
# Creating series of 2darray
snd = pd.Series(Data)
Copy the code
Output:
The name of the Series index and the name of the value (equivalent to the names of the two vectors)
Obj3. name="population" obj3.index. Name ="ind" obj3 Out[23]: ind b 2.0 a 1.0 d NaN name: population, dtype: float64Copy the code
DataFrames:
DataFrames are two-dimensional (2-D) data structures consisting of rows and columns defined in panda. Is a typical tabular data, both row index and column index. Equivalent to a large dictionary whose key is a column index and whose value is a Series; The Series that make up each of these indexes share a Series index.
Code 1:
# Program to Create DataFrame
# Import Library
import pandas as pd
# Create DataFrame with Data
a = pd.DataFrame(Data)
Copy the code
Here, the data could be:
-
One or more
* * * * dictionary -
One or more
* * * * series -
**2D-Numpy Ndarray**
Code 2: When data is a dictionary
# Program to Create Data Frame with two dictionaries
# Define Dictionary 1
dict1 ={'a':1, 'b':2, 'c':3, 'd':4}
# Define Dictionary 2
dict2 ={'a':5, 'b':6, 'c':7, 'd':8, 'e':9}
# Define Data with dict1 and dict2
Data = {'first':dict1, 'second':dict2}
# Create DataFrame
df = pd.DataFrame(Data)
Copy the code
Output:
DataFrame takes a series by default by column index; (In series, a value is obtained by index by default)
Df ["popular"] or df. Popular Out[37]: 0 8 1 9 2 10 3 11 Name: dtype: int64Copy the code
Code 3: When data is a sequence
# Program to create Dataframe of three series import pandas as pd # Define series 1 s1 = pd.Series([1, 3, 4, 5, 6, 2, 5]) # Define s2 = pd.series (['a', 'b', 'c', 'd', 'e']) # Define Data Data ={'first':s1, 'second':s2, 'third':s3} # Create DataFrame dfseries = pd.DataFrame(Data)Copy the code
Output:
The DataFrame indirectly acquires the row vector via IX, which is also a series whose index is the column index of the original DF.
df.loc[2]
Out[40]:
cities bj
year 2003
popular 10
Name: 2, dtype: object
Copy the code
Note: When creating dataframes for 2D arrays, one constraint must be maintained — the dimensions of the 2D arrays must be the same.
# Program to create DataFrame from 2D array
# Import Library
import pandas as pd
# Define 2d array 1
d1 =[[2, 3, 4], [5, 6, 7]]
# Define 2d array 2
d2 =[[2, 4, 8], [1, 3, 9]]
# Define Data
Data ={'first': d1, 'second': d2}
# Create DataFrame
df2d = pd.DataFrame(Data)
Copy the code
Output: