Pandas trong Python

Import library

import pandas as pd
import numpy as np # import numpy if necessary

Create a dataframe

# FROM A LIST
pd.DataFrame(a_list, colummns=['col_name'])
# FROM A DICTIONARY
names = ['John', 'David', 'Kenny', 'Alex', 'Piter']
ages =  [10, 20, 21, 18, 11]
marks = [8, 9, 10, 6, 8]
city = ['London', 'Paris', 'Phila', 'New York', 'DC']

my_dict = {'Name':names, 'Ages':ages, 'Marks':marks, 'Place': city}
students = pd.DataFrame(my_dict)

print (students)

MultiIndex

arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo'], ['one', 'two', 'one', 'two', 'one', 'two']]
index = pd.MultiIndex.from_arrays(arrays)
df = pd.DataFrame(np.random.randn(3, 6), index=['A', 'B', 'C'], columns=index)

print (df)
© 2019-2020 Phan Nhật Chánh