2
2
** The easy way to write your own flavor of Pandas**
3
3
4
4
Pandas added an new (simple) API to register accessors with Pandas objects.
5
- This package adds support for registering methods as well.
5
+ This package does two things:
6
+ 1 . adds support for registering methods as well.
7
+ 2 . makes each of these functions backwards compatible with older versions of Pandas.
6
8
7
- * What does this mean?*
9
+ *** What does this mean?** *
8
10
9
- It's super easy to custom functionality to Pandas DataFrames and Series.
11
+ It is now simpler to add custom functionality to Pandas DataFrames and Series.
10
12
11
13
Import this package. Write a simple python function. Register the function using one of the following decorators.
12
14
13
- * Why?*
15
+ *** Why?** *
14
16
15
17
Pandas is super handy. Its general purpose is to be a "flexible and powerful data analysis/manipulation library".
16
18
17
- ** Pandas Flavor** allows you to tailor Pandas to specific fields or use cases.
19
+ ** Pandas Flavor** allows you add functionality that tailors Pandas to specific fields or use cases.
18
20
19
21
Maybe you want to add new write methods to the Pandas DataFrame? Maybe you want custom plot functionality? Maybe something else?
20
22
@@ -37,21 +39,43 @@ class MyFlavor(object):
37
39
def __init__ (self , data ):
38
40
self ._data
39
41
40
- def is_cool (self ):
41
- """ Is my accessor cool?"""
42
- return True
42
+ def row_by_value (self , col , value ):
43
+ """ Slice out row from DataFrame by a value."""
44
+ return self ._data[self ._data[col] == value].squeeze()
45
+
46
+ ```
47
+
48
+ Every dataframe now has this accessor as an attribute.
49
+ ``` python
43
50
44
51
# DataFrame.
45
- df = DataFrame()
52
+ df = DataFrame(data = {
53
+ " x" : [10 , 20 , 25 ],
54
+ " y" : [0 , 2 , 5 ]
55
+ })
56
+
57
+ # Print DataFrame
58
+ print (df)
59
+
60
+ # x y
61
+ # 0 10 0
62
+ # 1 20 2
63
+ # 2 25 5
64
+
65
+ # Access this functionality
66
+ df.my_flavor.row_by_value(' x' , 10 )
46
67
47
68
# Access this functionality
48
- df.my_flavor.is_cool()
49
- # Returns True
50
- True
69
+ df.row_by_value(' x' , 10 )
70
+
71
+ # x 10
72
+ # y 0
73
+ # Name: 0, dtype: int64
51
74
```
52
75
53
- To see this in action, check out [ pdvega] ( https://github.com/jakevdp/pdvega ) ! this
54
- library adds a new plotting accessor for making Vega plots from pandas data.
76
+ To see this in action, check out [ pdvega] ( https://github.com/jakevdp/pdvega ) and
77
+ [ PhyloPandas] ( https://github.com/Zsailer/phylopandas ) !
78
+
55
79
56
80
## Register methods
57
81
@@ -63,19 +87,31 @@ import pandas as pd
63
87
import pandas_flavor as pf
64
88
65
89
@pf.register_dataframe_method
66
- def is_cool (df ):
67
- """ Is my dataframe cool?"""
68
- return True
90
+ def row_by_value (df , col , value ):
91
+ """ Slice out row from DataFrame by a value."""
92
+ return df[df[col] == value].squeeze()
93
+
94
+ ```
69
95
96
+ ``` python
70
97
# DataFrame.
71
- df = DataFrame()
98
+ df = DataFrame(data = {
99
+ " x" : [10 , 20 , 25 ],
100
+ " y" : [0 , 2 , 5 ]
101
+ })
102
+
103
+ # Print DataFrame
104
+ print (df)
105
+
106
+ # x y
107
+ # 0 10 0
108
+ # 1 20 2
109
+ # 2 25 5
72
110
73
111
# Access this functionality
74
- df.is_cool()
75
- # Returns True
76
- True
77
- ```
112
+ df.row_by_value(' x' , 10 )
78
113
79
- To see in action, check out [ PhyloPandas] ( https://github.com/Zsailer/phylopandas ) .
80
- This library adds extra ` to_ ` methods for writing DataFrames to various biological
81
- sequencing file formats.
114
+ # x 10
115
+ # y 0
116
+ # Name: 0, dtype: int64
117
+ ```
0 commit comments