Discuss different ways of creating a series in python programming
languages using panda library. Justify your answer with the help of
suitable example
1 It is possible to create empty dataset
df = pandas.DataFrame()
2 Data frame from a list
lst = ['a', 'b', 'c']
df = pandas.DataFrame(lst)
3 Create dataset from a dictionary
d = {'country': ['France', 'Spain', 'Italy'], 'capital': ['Paris', 'Madrid', 'Rome']}
df = pandas.DataFrame(d)
4 Read from a csv file
df = pandas.read_csv('filename.csv')
5 List if dictionaries
data = [{'a': 1, 'b': 2, 'c':3},
{'a':10, 'b': 20, 'c': 30}]
df = pandas.DataFrame(data)
6 Using zip() function
Countries = ['France', 'Spain', 'Italy']
Capitals = ['Paris', 'Madrid', 'Rome']
lst = list(zip(Countries, Capitals))
df = pandas.DataFrame(lst, columns = ['Country', 'Capital'])
7 From a series
d = {'one' : pandas.Series([10, 20, 30, 40],
index =['a', 'b', 'c', 'd']),
'two' : pandas.Series([10, 20, 30, 40],
index =['a', 'b', 'c', 'd'])}
df = pandas.DataFrame(d)
Comments
Leave a comment