"A value is trying to be set on a copy of a slice from a DataFrame" is a warning. SO contains many posts on this subject.
df.assign
was added in Pandas 0.16 and is a good way to avoid this warning.
quarter = {"Q1": "Mar", "Q2": "Jun", "Q3": "Sep", "Q4": "Dec"}
df = pd.DataFrame({'period': ['Q1', 'Q2', 'Q3', 'Q4', 'Q5'], 'qtr': [1, 2, 3, 4, 5]})
df
period qtr
0 Q1 1
1 Q2 2
2 Q3 3
3 Q4 4
4 Q5 5
df = df.assign(period=[quarter.get(q, q) for q in df.period])
# Unmapped values unchanged.
>>> df
period qtr
0 Mar 1
1 Jun 2
2 Sep 3
3 Dec 4
4 Q5 5
df = pd.DataFrame({'period': ['Q1', 'Q2', 'Q3', 'Q4', 'Q5'], 'qtr': [1, 2, 3, 4, 5]})
df = df.assign(period=df.period.map(quarter))
# Unmapped values get `NaN`.
>>> df
period qtr
0 Mar 1
1 Jun 2
2 Sep 3
3 Dec 4
4 NaN 5
Assign new columns to a DataFrame, returning a new object
(a copy) with all the original columns in addition to the new ones.
.. versionadded:: 0.16.0
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…