import numpy as np
import pandas as pd
df = pd. DataFrame( np. random. randn( 3 , 4 ) , index= list ( "abc" ) , columns= list ( "ABCD" ) )
df
A B C D a -1.191323 -0.774396 0.234996 -0.565204 b 0.042025 -0.003400 -1.517832 -0.729930 c 0.459724 0.341114 -0.070776 0.515991
df[ "E" ] = df. A + df. B
df
A B C D E a 1.241630 0.384139 -1.225968 -1.160028 1.625769 b -0.736530 -0.148695 -1.622824 -0.145651 -0.885224 c 0.370135 -1.130262 0.032585 -0.416405 -0.760128
df. insert( 1 , "insert" , 6 )
df
A insert B C D E a 1.241630 6 0.384139 -1.225968 -1.160028 1.625769 b -0.736530 6 -0.148695 -1.622824 -0.145651 -0.885224 c 0.370135 6 -1.130262 0.032585 -0.416405 -0.760128
df. assign( F = df[ "A" ] / df[ "B" ] )
A insert B C D E F a 1.241630 6 0.384139 -1.225968 -1.160028 1.625769 3.232243 b -0.736530 6 -0.148695 -1.622824 -0.145651 -0.885224 4.953305 c 0.370135 6 -1.130262 0.032585 -0.416405 -0.760128 -0.327477
del df[ "A" ]
df
B C D E a 0.224567 -1.049519 0.157046 -1.692286 b 1.359846 1.605034 0.213492 1.141026 c 0.206875 -0.037353 -0.092492 -0.673894
df. pop( "B" )
df
C D E a -1.049519 0.157046 -1.692286 b 1.605034 0.213492 1.141026 c -0.037353 -0.092492 -0.673894
del df. a
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-38-5c6c11f8ca66> in <module>
1 #删除行
----> 2 del df.a
AttributeError: a
del df. loc[ "c" , : ]
df
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-39-87bdf8cd1515> in <module>
1 #删除行
----> 2 del df.loc["c",:]
3 df
AttributeError: __delitem__
df. C = "bar"
df
C D E a bar 0.157046 -1.692286 b bar 0.213492 1.141026 c bar -0.092492 -0.673894
df. c = "barr"
df
C D E a bar 0.157046 -1.692286 b bar 0.213492 1.141026 c bar -0.092492 -0.673894
df. c
'barr'
df. loc[ "a" , : ] = "barr"
df
C D E a barr barr barr b bar 0.213492 1.14103 c bar -0.0924917 -0.673894
df
A insert B C D E a 1.241630 6 0.384139 -1.225968 -1.160028 1.625769 b -0.736530 6 -0.148695 -1.622824 -0.145651 -0.885224 c 0.370135 6 -1.130262 0.032585 -0.416405 -0.760128
df[ "A" ]
a 1.241630
b -0.736530
c 0.370135
Name: A, dtype: float64
df. loc[ "a" ]
A 1.241630
insert 6.000000
B 0.384139
C -1.225968
D -1.160028
E 1.625769
Name: a, dtype: float64
df. iloc[ 0 ]
A 1.241630
insert 6.000000
B 0.384139
C -1.225968
D -1.160028
E 1.625769
Name: a, dtype: float64
df[ 0 : 2 ]
A insert B C D E a 1.24163 6 0.384139 -1.225968 -1.160028 1.625769 b -0.73653 6 -0.148695 -1.622824 -0.145651 -0.885224
df[ 0 : - 1 ]
A insert B C D E a 1.24163 6 0.384139 -1.225968 -1.160028 1.625769 b -0.73653 6 -0.148695 -1.622824 -0.145651 -0.885224
df. loc[ "a" , "A" ]
1.2416298344644054
df. iloc[ 0 , 0 ]
1.2416298344644054
df. loc[ [ "a" , "c" ] , [ "A" , "C" ] ]
A C a 1.241630 -1.225968 c 0.370135 0.032585
df. iloc[ [ 0 , 2 ] , [ 0 , 3 ] ]
A C a 1.241630 -1.225968 c 0.370135 0.032585
df. iloc[ [ 0 , 2 ] ]
A insert B C D E a 1.241630 6 0.384139 -1.225968 -1.160028 1.625769 c 0.370135 6 -1.130262 0.032585 -0.416405 -0.760128
df. loc[ [ "a" , "c" ] ]
A insert B C D E a 1.241630 6 0.384139 -1.225968 -1.160028 1.625769 c 0.370135 6 -1.130262 0.032585 -0.416405 -0.760128
df[ [ 0 , 2 ] , [ "A" , "C" ] ]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-81-970a7eaa38bd> in <module>
----> 1 df[[0,2],["A","C"]]
~/miniconda3/lib/python3.7/site-packages/pandas/core/frame.py in __getitem__(self, key)
2798 if self.columns.nlevels > 1:
2799 return self._getitem_multilevel(key)
-> 2800 indexer = self.columns.get_loc(key)
2801 if is_integer(indexer):
2802 indexer = [indexer]
~/miniconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2644 )
2645 try:
-> 2646 return self._engine.get_loc(key)
2647 except KeyError:
2648 return self._engine.get_loc(self._maybe_cast_indexer(key))
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
TypeError: '([0, 2], ['A', 'C'])' is an invalid key
df
A B C D a -1.191323 -0.774396 0.234996 -0.565204 b 0.042025 -0.003400 -1.517832 -0.729930 c 0.459724 0.341114 -0.070776 0.515991
df[ df[ "A" ] > 0 ]
A B C D b 0.042025 -0.003400 -1.517832 -0.729930 c 0.459724 0.341114 -0.070776 0.515991
df[ ( df. A > 0 ) & ( df. D > 0 ) ]
A B C D c 0.459724 0.341114 -0.070776 0.515991
df[ df > 0 ]
A B C D a NaN NaN 0.234996 NaN b 0.042025 NaN NaN NaN c 0.459724 0.341114 NaN 0.515991
df[ "E" ] = [ "AA" , "BB" , "CC" ]
df
A B C D E a -1.191323 -0.774396 0.234996 -0.565204 AA b 0.042025 -0.003400 -1.517832 -0.729930 BB c 0.459724 0.341114 -0.070776 0.515991 CC
df[ df. E. isin( [ "AA" , "CC" ] ) ]
A B C D E a -1.191323 -0.774396 0.234996 -0.565204 AA c 0.459724 0.341114 -0.070776 0.515991 CC