CREATE A DATAFRAME FROM DICT OF NDARRAYS OR LISTS
Create a DataFrame from Dict of ndarrays / Lists
All the ndarrays must be of same length. If index is passed, then the length of the index should equal to the length of the arrays.
If no index is passed, then by default, index will be range(n), where n is the array length.
import pandas as pd
data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
df = pd.DataFrame(data)
df
Name | Age | |
---|---|---|
0 | Tom | 28 |
1 | Jack | 34 |
2 | Steve | 29 |
3 | Ricky | 42 |
Observe the values 0,1,2,3. They are the default index assigned to each using the function range(n)
Indexed DataFrame using arrays.
data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
df = pd.DataFrame(data, index=['rank1','rank2','rank3','rank4'])
df
Name | Age | |
---|---|---|
rank1 | Tom | 28 |
rank2 | Jack | 34 |
rank3 | Steve | 29 |
rank4 | Ricky | 42 |
the index parameter assigns an index to each row