kopia lustrzana https://github.com/animator/learn-python
Update pandas_series.md
rodzic
4afd4a98f2
commit
4916b99a3a
|
@ -13,14 +13,14 @@ import pandas as pd
|
|||
|
||||
s1 = pd.Series([4, 5, 2, 3])
|
||||
print(s1)
|
||||
|
||||
#Output:
|
||||
#0 4
|
||||
#1 5
|
||||
#2 2
|
||||
#3 3
|
||||
#dtype: int64
|
||||
|
||||
```
|
||||
```
|
||||
Output:
|
||||
0 4
|
||||
1 5
|
||||
2 2
|
||||
3 3
|
||||
dtype: int64
|
||||
```
|
||||
|
||||
### Series from a Dictionary
|
||||
|
@ -31,12 +31,13 @@ import pandas as pd
|
|||
|
||||
s2 = pd.Series({'A': 1, 'B': 2, 'C': 3})
|
||||
print(s2)
|
||||
|
||||
#Output:
|
||||
#A 1
|
||||
#B 2
|
||||
#C 3
|
||||
#dtype: int64
|
||||
```
|
||||
```
|
||||
Output:
|
||||
A 1
|
||||
B 2
|
||||
C 3
|
||||
dtype: int64
|
||||
```
|
||||
|
||||
|
||||
|
@ -50,12 +51,13 @@ import pandas as pd
|
|||
|
||||
s4 = pd.Series([1, 2, 3], index=['a', 'b', 'c'], dtype='float64')
|
||||
print(s4)
|
||||
|
||||
#output
|
||||
#a 1.0
|
||||
#b 2.0
|
||||
#c 3.0
|
||||
#dtype: float64
|
||||
```
|
||||
```
|
||||
Output:
|
||||
a 1.0
|
||||
b 2.0
|
||||
c 3.0
|
||||
dtype: float64
|
||||
```
|
||||
|
||||
### Specifying NaN Values:
|
||||
|
@ -66,13 +68,13 @@ print(s4)
|
|||
import pandas as pd
|
||||
s3=pd.Series([1,np.Nan,2])
|
||||
print(s3)
|
||||
|
||||
#output:
|
||||
|
||||
#0 1.0
|
||||
#1 NaN
|
||||
#2 2.0
|
||||
#dtype: float64
|
||||
```
|
||||
```
|
||||
Output:
|
||||
0 1.0
|
||||
1 NaN
|
||||
2 2.0
|
||||
dtype: float64
|
||||
```
|
||||
|
||||
|
||||
|
@ -86,15 +88,15 @@ import pandas as pd
|
|||
a=np.arange(1,5) # [1,2,3,4]
|
||||
s5=pd.Series(data=a**2,index=a)
|
||||
print(s5)
|
||||
|
||||
#output:
|
||||
#1 1
|
||||
#2 4
|
||||
#3 9
|
||||
#4 16
|
||||
#dtype: int64
|
||||
```
|
||||
|
||||
```
|
||||
Output:
|
||||
1 1
|
||||
2 4
|
||||
3 9
|
||||
4 16
|
||||
dtype: int64
|
||||
```
|
||||
|
||||
## Series Object Attributes
|
||||
|
||||
|
@ -123,9 +125,10 @@ import pandas as pd
|
|||
|
||||
s7 = pd.Series(data=[13, 45, 67, 89], index=['A', 'B', 'C', 'D'])
|
||||
print(s7['A'])
|
||||
|
||||
#output
|
||||
#13
|
||||
```
|
||||
```
|
||||
Output:
|
||||
13
|
||||
```
|
||||
|
||||
|
||||
|
@ -142,12 +145,14 @@ import pandas as pd
|
|||
|
||||
s = pd.Series(data=[13, 45, 67, 89], index=['A', 'B', 'C', 'D'])
|
||||
print(s[:2])
|
||||
```
|
||||
```
|
||||
Output:
|
||||
A 13
|
||||
B 45
|
||||
dtype: int64
|
||||
|
||||
#Output
|
||||
#A 13
|
||||
#B 45
|
||||
#dtype: int64
|
||||
#This example demonstrates that the first two elements (positions 0 and 1) are returned, regardless of their custom index labels.
|
||||
This example demonstrates that the first two elements (positions 0 and 1) are returned, regardless of their custom index labels.
|
||||
```
|
||||
|
||||
|
||||
|
@ -165,12 +170,13 @@ s8 = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
|
|||
s8['a'] = 100
|
||||
s8.index = ['x', 'y', 'z']
|
||||
print(s8)
|
||||
|
||||
#output
|
||||
#x 100
|
||||
#y 20
|
||||
#z 30
|
||||
#dtype: int64
|
||||
```
|
||||
```
|
||||
Output:
|
||||
x 100
|
||||
y 20
|
||||
z 30
|
||||
dtype: int64
|
||||
```
|
||||
|
||||
**Note: Series object are value-mutable but size immutable objects.**
|
||||
|
@ -183,18 +189,21 @@ import pandas as pd
|
|||
s9 = pd.Series([1, 2, 3])
|
||||
print("addition:", s9 + 5)
|
||||
print("subtraction:", s9 - 2)
|
||||
```
|
||||
```
|
||||
output:
|
||||
|
||||
#output:
|
||||
#addition:
|
||||
#0 6
|
||||
#1 7
|
||||
#2 8
|
||||
#dtype: int64
|
||||
#subtraction:
|
||||
#0 -1
|
||||
#1 0
|
||||
#2 1
|
||||
#dtype: int64
|
||||
addition:
|
||||
0 6
|
||||
1 7
|
||||
2 8
|
||||
dtype: int64
|
||||
|
||||
subtraction:
|
||||
0 -1
|
||||
1 0
|
||||
2 1
|
||||
dtype: int64
|
||||
```
|
||||
|
||||
### Arthmetic on series object
|
||||
|
@ -206,18 +215,21 @@ s11 = pd.Series([4, 5, 6])
|
|||
print("addition:", s10 + s11)
|
||||
|
||||
print("multiplication:", s10 * s11)
|
||||
```
|
||||
```
|
||||
output:
|
||||
|
||||
#output:
|
||||
#addition:
|
||||
#0 5
|
||||
#1 7
|
||||
#2 9
|
||||
#dtype: int64
|
||||
#multiplication:
|
||||
#0 4
|
||||
#1 10
|
||||
#2 18
|
||||
#dtype: int64
|
||||
addition:
|
||||
0 5
|
||||
1 7
|
||||
2 9
|
||||
dtype: int64
|
||||
|
||||
multiplication:
|
||||
0 4
|
||||
1 10
|
||||
2 18
|
||||
dtype: int64
|
||||
```
|
||||
|
||||
Here one thing we should keep in mind that both the series object should have same indexes otherwise it will return NaN value to all the indexes of two series object .
|
||||
|
@ -236,17 +248,18 @@ import pandas as pd
|
|||
s12 = pd.Series([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
|
||||
print(s12.head(3))
|
||||
print(s12.tail(3))
|
||||
```
|
||||
```
|
||||
Output:
|
||||
0 10
|
||||
1 20
|
||||
2 30
|
||||
dtype: int64
|
||||
|
||||
#output
|
||||
#0 10
|
||||
#1 20
|
||||
#2 30
|
||||
#dtype: int64
|
||||
|
||||
#7 80
|
||||
#8 90
|
||||
#9 100
|
||||
#dtype: int64
|
||||
7 80
|
||||
8 90
|
||||
9 100
|
||||
dtype: int64
|
||||
```
|
||||
If you dont provide any value to n the by default it give results for `n=5`.
|
||||
|
||||
|
@ -263,20 +276,22 @@ s13 = pd.Series([3, 1, 2], index=['c', 'a', 'b'])
|
|||
print(s13.sort_values())
|
||||
print(s13.sort_index())
|
||||
print(s13.drop('a'))
|
||||
#Output
|
||||
#a 1
|
||||
#b 2
|
||||
#c 3
|
||||
#dtype: int64
|
||||
```
|
||||
```
|
||||
Output:
|
||||
a 1
|
||||
b 2
|
||||
c 3
|
||||
dtype: int64
|
||||
|
||||
#a 1
|
||||
#b 2
|
||||
#c 3
|
||||
#dtype: int64
|
||||
a 1
|
||||
b 2
|
||||
c 3
|
||||
dtype: int64
|
||||
|
||||
#c 3
|
||||
#b 2
|
||||
#dtype: int64
|
||||
c 3
|
||||
b 2
|
||||
dtype: int64
|
||||
```
|
||||
|
||||
## Conclusion
|
||||
|
|
Ładowanie…
Reference in New Issue