I'm new to Pandas, and constantly getting the infamous SettingWithCopyWarning. If I have code like the following I get the warning:
import pandas as pd
import numpy as np
t1 = pd.DataFrame({'A':np.random.randn(8), 'B': np.random.randint(0,4,8)})
table = t1[t1['B'] < 3]
print(table)
table.sort(['B'], inplace=True)
print(table)
In this case, is it just warning me that the original data frame (ie, 't1') will not be sorted when I run the sort?
Is there a more accepted way to perform this sort of operation (getting a slice of a data frame, and then applying functions on that slice)?
Why not make a copy of the slice? Then you will not have the warning.
a = table.copy()
You can read more in the answers to this post How to deal with SettingWithCopyWarning in Pandas? .
You can also avoid to use inplace
, in this case the copy will be made behind the scene for you.
t1 = pd.DataFrame({'A':np.random.randn(8), 'B': np.random.randint(0,4,8)})
table = t1[t1['B'] < 3]
table = table.sort(['B'])
print(table)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.