Update pandas_series.md

pull/1100/head
anamika123 2024-06-22 18:19:33 +05:30
rodzic 4afd4a98f2
commit 4916b99a3a
1 zmienionych plików z 108 dodań i 93 usunięć

Wyświetl plik

@ -13,14 +13,14 @@ import pandas as pd
s1 = pd.Series([4, 5, 2, 3]) s1 = pd.Series([4, 5, 2, 3])
print(s1) print(s1)
```
#Output: ```
#0 4 Output:
#1 5 0 4
#2 2 1 5
#3 3 2 2
#dtype: int64 3 3
dtype: int64
``` ```
### Series from a Dictionary ### Series from a Dictionary
@ -31,12 +31,13 @@ import pandas as pd
s2 = pd.Series({'A': 1, 'B': 2, 'C': 3}) s2 = pd.Series({'A': 1, 'B': 2, 'C': 3})
print(s2) print(s2)
```
#Output: ```
#A 1 Output:
#B 2 A 1
#C 3 B 2
#dtype: int64 C 3
dtype: int64
``` ```
@ -50,12 +51,13 @@ import pandas as pd
s4 = pd.Series([1, 2, 3], index=['a', 'b', 'c'], dtype='float64') s4 = pd.Series([1, 2, 3], index=['a', 'b', 'c'], dtype='float64')
print(s4) print(s4)
```
#output ```
#a 1.0 Output:
#b 2.0 a 1.0
#c 3.0 b 2.0
#dtype: float64 c 3.0
dtype: float64
``` ```
### Specifying NaN Values: ### Specifying NaN Values:
@ -66,13 +68,13 @@ print(s4)
import pandas as pd import pandas as pd
s3=pd.Series([1,np.Nan,2]) s3=pd.Series([1,np.Nan,2])
print(s3) print(s3)
```
#output: ```
Output:
#0 1.0 0 1.0
#1 NaN 1 NaN
#2 2.0 2 2.0
#dtype: float64 dtype: float64
``` ```
@ -86,15 +88,15 @@ import pandas as pd
a=np.arange(1,5) # [1,2,3,4] a=np.arange(1,5) # [1,2,3,4]
s5=pd.Series(data=a**2,index=a) s5=pd.Series(data=a**2,index=a)
print(s5) 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 ## Series Object Attributes
@ -123,9 +125,10 @@ import pandas as pd
s7 = pd.Series(data=[13, 45, 67, 89], index=['A', 'B', 'C', 'D']) s7 = pd.Series(data=[13, 45, 67, 89], index=['A', 'B', 'C', 'D'])
print(s7['A']) 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']) s = pd.Series(data=[13, 45, 67, 89], index=['A', 'B', 'C', 'D'])
print(s[:2]) print(s[:2])
```
```
Output:
A 13
B 45
dtype: int64
#Output This example demonstrates that the first two elements (positions 0 and 1) are returned, regardless of their custom index labels.
#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.
``` ```
@ -165,12 +170,13 @@ s8 = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
s8['a'] = 100 s8['a'] = 100
s8.index = ['x', 'y', 'z'] s8.index = ['x', 'y', 'z']
print(s8) print(s8)
```
#output ```
#x 100 Output:
#y 20 x 100
#z 30 y 20
#dtype: int64 z 30
dtype: int64
``` ```
**Note: Series object are value-mutable but size immutable objects.** **Note: Series object are value-mutable but size immutable objects.**
@ -183,18 +189,21 @@ import pandas as pd
s9 = pd.Series([1, 2, 3]) s9 = pd.Series([1, 2, 3])
print("addition:", s9 + 5) print("addition:", s9 + 5)
print("subtraction:", s9 - 2) print("subtraction:", s9 - 2)
```
```
output:
#output: addition:
#addition: 0 6
#0 6 1 7
#1 7 2 8
#2 8 dtype: int64
#dtype: int64
#subtraction: subtraction:
#0 -1 0 -1
#1 0 1 0
#2 1 2 1
#dtype: int64 dtype: int64
``` ```
### Arthmetic on series object ### Arthmetic on series object
@ -206,18 +215,21 @@ s11 = pd.Series([4, 5, 6])
print("addition:", s10 + s11) print("addition:", s10 + s11)
print("multiplication:", s10 * s11) print("multiplication:", s10 * s11)
```
```
output:
#output: addition:
#addition: 0 5
#0 5 1 7
#1 7 2 9
#2 9 dtype: int64
#dtype: int64
#multiplication: multiplication:
#0 4 0 4
#1 10 1 10
#2 18 2 18
#dtype: int64 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 . 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]) s12 = pd.Series([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print(s12.head(3)) print(s12.head(3))
print(s12.tail(3)) print(s12.tail(3))
```
```
Output:
0 10
1 20
2 30
dtype: int64
#output 7 80
#0 10 8 90
#1 20 9 100
#2 30 dtype: int64
#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`. 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_values())
print(s13.sort_index()) print(s13.sort_index())
print(s13.drop('a')) print(s13.drop('a'))
#Output ```
#a 1 ```
#b 2 Output:
#c 3 a 1
#dtype: int64 b 2
c 3
dtype: int64
#a 1 a 1
#b 2 b 2
#c 3 c 3
#dtype: int64 dtype: int64
#c 3 c 3
#b 2 b 2
#dtype: int64 dtype: int64
``` ```
## Conclusion ## Conclusion