Why is this? I'm going to make 4 dataframes in pandas:
>>> df = pd.DataFrame({"A": ["One","Two","Three"], "B": ["Two","Three","Four"], "C": ["Three","Four","Five"], "D": ["Four","Five","Six"]})
>>> df
A B C D
0 One Two Three Four
1 Two Three Four Five
2 Three Four Five Six
>>> df["C"][1] = "One Hundred"
Everything worked well; now let's do two columns first then we add two columns, one with "" and the other one with NaN
>>> df = pd.DataFrame({"A": [1,2,3], "B": [2,3,4]})
>>> df
A B
0 1 2
1 2 3
2 3 4
>>> df["C"] = ""
>>> df["D"] = pd.np.nan
>>> df
A B C D
0 1 2 NaN
1 2 3 NaN
2 3 4 NaN
>>> df["C"][1] = "hello"
Warning (from warnings module):
File "__main__", line 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
A Warning shows up! Ok: (this is the question: what is that warning for?) But let's continue:
now I do this:
>>> df = pd.DataFrame({"A": [1,2,3], "B": [2,3,4], "C": [3,4,5], "D": [4,5,6]})
>>> df
A B C D
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
>>> df["C"][1] = 100
and no Warning appears. ok.
Now let's trigger the warning again:
>>> df = pd.DataFrame({"A": [1,2,3], "B": [2,3,4]})
>>> df["C"] = ""
>>> df["D"] = pd.np.nan
>>> df
A B C D
0 1 2 NaN
1 2 3 NaN
2 3 4 NaN
>>> df["C"][1] = "hello"
>>>
NO WARNING THIS TIME!?
I'm using IDLE 3.5.2 , Python version: 3.5.2...
Is this a bug? I can't tell because I'm studying.
Should I write a new separate DataFrame with all the column and then just equal it to the df's column each time?
Should I make a python list?...
Is there a way to traverse and edit over the original dataframe without the warning?
Why isn't that warning popping up every time?
Thanks for your time.