pandas 注意点


In [18]: a = a.head(10)                                                                                                                                                                                     

In [19]: a                                                                                                                                                                                                  
Out[19]: 
            Unnamed: 0  temp               second  temp_interval
0  2019-03-25 02:54:21    55  2019-03-25 02:54:21              0
1  2019-03-25 02:54:22    55  2019-03-25 02:54:21              0
2  2019-03-25 02:54:23    55  2019-03-25 02:54:21              0
3  2019-03-25 02:54:24    55  2019-03-25 02:54:21              0
4  2019-03-25 02:54:25    55  2019-03-25 02:54:21              0
5  2019-03-25 02:54:26    55  2019-03-25 02:54:21              0
6  2019-03-25 02:54:27    55  2019-03-25 02:54:21              0
7  2019-03-25 02:54:28    55  2019-03-25 02:54:21              0
8  2019-03-25 02:54:29    55  2019-03-25 02:54:21              0
9  2019-03-25 02:54:30    55  2019-03-25 02:54:21              0

In [20]: a.temp_interval = [0, 1, 1, 1, 32, 3, 34, -1, -2, -3]                                                                                                                                              
/home/yinhaibo/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py:4405: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self[name] = value

In [21]: a[a.temp_interval < -2] = 0                                                                                                                                                                        
/home/yinhaibo/anaconda3/bin/ipython:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  #!/home/yinhaibo/anaconda3/bin/python
/home/yinhaibo/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py:543: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self.obj[item] = s

In [22]: a                                                                                                                                                                                                  
Out[22]: 
            Unnamed: 0  temp               second  temp_interval
0  2019-03-25 02:54:21    55  2019-03-25 02:54:21              0
1  2019-03-25 02:54:22    55  2019-03-25 02:54:21              1
2  2019-03-25 02:54:23    55  2019-03-25 02:54:21              1
3  2019-03-25 02:54:24    55  2019-03-25 02:54:21              1
4  2019-03-25 02:54:25    55  2019-03-25 02:54:21             32
5  2019-03-25 02:54:26    55  2019-03-25 02:54:21              3
6  2019-03-25 02:54:27    55  2019-03-25 02:54:21             34
7  2019-03-25 02:54:28    55  2019-03-25 02:54:21             -1
8  2019-03-25 02:54:29    55  2019-03-25 02:54:21             -2
9                    0     0                    0              0

In [23]: a[a.temp_interval < -1].iloc[:, 3] = 0                                                                                                                                                             
/home/yinhaibo/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py:543: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self.obj[item] = s

In [24]: a                                                                                                                                                                                                  
Out[24]: 
            Unnamed: 0  temp               second  temp_interval
0  2019-03-25 02:54:21    55  2019-03-25 02:54:21              0
1  2019-03-25 02:54:22    55  2019-03-25 02:54:21              1
2  2019-03-25 02:54:23    55  2019-03-25 02:54:21              1
3  2019-03-25 02:54:24    55  2019-03-25 02:54:21              1
4  2019-03-25 02:54:25    55  2019-03-25 02:54:21             32
5  2019-03-25 02:54:26    55  2019-03-25 02:54:21              3
6  2019-03-25 02:54:27    55  2019-03-25 02:54:21             34
7  2019-03-25 02:54:28    55  2019-03-25 02:54:21             -1
8  2019-03-25 02:54:29    55  2019-03-25 02:54:21             -2
9                    0     0                    0              0

In [25]: a[a.temp_interval < -1][] = 0                                                                                                                                                                      

pandas 注意点_第1张图片

python3

pandas 注意点_第2张图片

python2

pandas 注意点_第3张图片

变特定列值的方法


In [28]: b.iloc[list(b[b.b>5].index), :]                                                                                                                                                                    
Out[28]: 
    a                 time   b
1  99                   99  99
3   3  2018-01-04 00:00:00   6

In [29]: b.iloc[list(b[b.b>5].index), :].b                                                                                                                                                                  
Out[29]: 
1    99
3     6
Name: b, dtype: int64

In [30]: b.iloc[list(b[b.b>5].index), :].b = 99999                                                                                                                                                          
/home/yinhaibo/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py:4405: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self[name] = value

In [31]: b                                                                                                                                                                                                  
Out[31]: 
    a                 time   b
0   3  2018-01-01 00:00:00   5
1  99                   99  99
2   0                    0   0
3   3  2018-01-04 00:00:00   6
4   0                    0   0

In [32]: b.loc[list(b[b.b>5].index), 'b'] = 99999                                                                                                                                                           

In [33]: b                                                                                                                                                                                                  
Out[33]: 
    a                 time      b
0   3  2018-01-01 00:00:00      5
1  99                   99  99999
2   0                    0      0
3   3  2018-01-04 00:00:00  99999
4   0                    0      0

In [34]: b[b.b < 6].b = 555                                                                                                                                                                                 
/home/yinhaibo/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py:4405: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self[name] = value

In [35]: b[b.b < 6].loc[:, 'b'] = 55555555                                                                                                                                                                  
/home/yinhaibo/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py:543: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self.obj[item] = s

In [36]: b                                                                                                                                                                                                  
Out[36]: 
    a                 time      b
0   3  2018-01-01 00:00:00      5
1  99                   99  99999
2   0                    0      0
3   3  2018-01-04 00:00:00  99999
4   0                    0      0

In [37]: b.loc[:, 'b'][b.b < 6] = 55555555                                                                                                                                                                  
/home/yinhaibo/anaconda3/bin/ipython:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  #!/home/yinhaibo/anaconda3/bin/python

In [38]: b                                                                                                                                                                                                  
Out[38]: 
    a                 time         b
0   3  2018-01-01 00:00:00  55555555
1  99                   99     99999
2   0                    0  55555555
3   3  2018-01-04 00:00:00     99999
4   0                    0  55555555

In [39]: b.iloc[:, 2] = 666                                                                                                                                                                                 

In [40]: b                                                                                                                                                                                                  
Out[40]: 
    a                 time    b
0   3  2018-01-01 00:00:00  666
1  99                   99  666
2   0                    0  666
3   3  2018-01-04 00:00:00  666
4   0                    0  666
	# data.drop(['temp', 'second'], axis=1, inplace=True)
    #
	# data[data.temp_interval < -3] = 0
	# data[data.temp_interval > 0] = 0

	###################################################


	# data.loc[:, 'temp_interval'][data.temp_interval < -3] = 0
	# data.loc[:, 'temp_interval'][data.temp_interval > 0] = 0
	# data.drop(['temp', 'second'], axis=1, inplace=True)

	data.loc[list([data.temp_interval < -3].index), 'temp_interval'] = 0
	data.loc[list([data.temp_interval > 0].index), 'temp_interval'] = 0
	data.drop(['temp', 'second'], axis=1, inplace=True)

你可能感兴趣的:(应用)