To customize or other library functions used in Pandas objects, there are three important ways , let's discuss how to use these methods. Use the appropriate method depends on the function to which level ( DataFrame
rows or columns or elements).
- Table rational function application:
pipe()
- Row or column function is applied:
apply()
- Elements function application:
applymap()
First, on the whole DataFrame
perform operations
By the appropriate number of parameters and functions as a pipe parameters to perform custom operations
PANDAS Import AS PD Import numpy AS NP # Adder function to add two numbers together as arguments and returns the sum DEF Adder (ELE1, ele2): return ELE1 + ele2 DF = pd.DataFrame (np.random.randn ( . 5 , . 3 ), = Columns [ ' col1 ' , ' col2 ' , ' col3 ' ]) df.pipe (Adder, 2 ) # now use a custom function to operate DataFrame print (df)
Output:
col1 col2 col3
0 2.176704 2.219691 1.509360
1 2.222378 2.422167 3.953921
2 2.241096 1.135424 2.696432
3 2.355763 0.376672 1.182570
4 2.308743 2.714767 2.130288
Second,Row or column operations performed
Can be used apply()
a method in DataFrame
or Panel
of the shaft applying any function, it is the same with the descriptive statistics, using an optional axis
parameter. By default, the operation performed in columns , each column as the array.
Example -1
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3']) df.apply(np.mean) print(df)
Output:
col1 col2 col3
0 0.343569 -1.013287 1.131245
1 0.508922 -0.949778 -1.600569
2 -1.182331 -0.420703 -1.725400
3 0.860265 2.069038 -0.537648
4 0.876758 -0.238051 0.473992
By passing axis
parameters, can be performed on a row operation.
Example -2
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3']) df.apply(np.mean,axis=1) print(df)
输出结果:
col1 col2 col3
0 0.543255 -1.613418 -0.500731
1 0.976543 -1.135835 -0.719153
2 0.184282 -0.721153 -2.876206
3 0.447738 0.268062 -1.937888
4 -0.677673 0.177455 1.397360
Examples -3
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3']) df.apply(lambda x: x.max() - x.min()) print(df)
Output:
col1 col2 col3
0 -0.585206 -0.104938 1.424115
1 -0.326036 -1.444798 0.196849
2 -2.033478 1.682253 1.223152
3 -0.107015 0.499846 0.084127
4 -1.046964 -1.935617 -0.009919
Third, perform operations on elements
Not all of the functions can be quantified (not return another array of NumPy
the array, nor is any value), the DataFrame
methods of applymap()
and similar in the Series map()
accept any Python function, and return a single value.
Example -1
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3']) df['col1'].map(lambda x:x*100) print(df)
输出结果:
col1 col2 col3
0 0.629348 0.088467 -1.790702
1 -0.592595 0.184113 -1.524998
2 -0.419298 0.262369 -0.178849
3 -1.036930 1.103169 0.941882
4 -0.573333 -0.031056 0.315590
示例-2
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3']) df.applymap(lambda x:x*100) print(df)
输出结果:
output is as follows:
col1 col2 col3
0 17.670426 21.969052 -49.064031
1 22.237846 42.216693 195.392124
2 24.109576 -86.457646 69.643171
3 35.576312 -162.332803 -81.743023
4 30.874333 71.476717 13.028751