Python Pandas (II)
Python Pandas (II)
a Python dict
a Python list
a Python tuple
1 data
data takes various forms like ndarray, list, constants
2 index
Index values must be unique and hashable, same length
as data.
3 dtype
dtype is for data type. If None, data type will be inferred
4 copy
Copy data. Default False
Create an Empty Series
Example
Output
0a
1b
2c
3 d dtype: object
Output
a 0.0
b 1.0
c 2.0 dtype: float64
Eg.2 (with index)
import pandas as pd1
import numpy as np1
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = pd1.Series(data,index=['b','c','d','a'])
print(s)
Output
b 1.0
c 2.0
d NaN
a 0.0 dtype: float64
Note − Index order is persisted and the missing element is filled with NaN (Not a Number).
Create a Series from Scalar
If data is a scalar value, an index must be provided. The
value will be repeated to match the length of index
Eg-3
import pandas as pd1
import numpy as np1
s = pd1.Series(5, index=[0, 1, 2, 3])
print(s)
Output
0 5
1 5
2 5
3 5
Accessing Data from Series with Position
import pandas as pd
s = pd.Series([1,2,3,4,5],index =['a','b','c','d','e'])
#retrieve the first element
print (s[0])
Print( s[‘c’])
1
3
Accessing Data from Series
with indexing and slicing
Example 2
1
a. 1
b. 2
c. 3
dtype: int64
c3
d. 4
e. 5
dtype: int64
How to Read Text Files with Pandas?
We can read the text files by Using read_csv()
import pandas as pd
# read text file into pandas DataFrame
df = pd.read_csv("gfg.txt", sep=" ")
print(df)