Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过dataframe循环以更改列值-python_Python_Pandas - Fatal编程技术网

通过dataframe循环以更改列值-python

通过dataframe循环以更改列值-python,python,pandas,Python,Pandas,我有一个以月为列的df。月份以数值表示,即从1到12。我需要将它们理解为它们的实际文本等价物,例如1将被一月替换,2将被二月替换,依此类推 此时此刻,我正在通过每月单独声明来完成这项工作: cntryPerMonth.loc[cntryPerMonth['Month'] == 1.0, 'Month'] = 'January' cntryPerMonth.loc[cntryPerMonth['Month'] == 2.0, 'Month'] = 'February' cntryPerMonth.

我有一个以月为列的df。月份以数值表示,即从1到12。我需要将它们理解为它们的实际文本等价物,例如1将被一月替换,2将被二月替换,依此类推

此时此刻,我正在通过每月单独声明来完成这项工作:

cntryPerMonth.loc[cntryPerMonth['Month'] == 1.0, 'Month'] = 'January'
cntryPerMonth.loc[cntryPerMonth['Month'] == 2.0, 'Month'] = 'February'
cntryPerMonth.loc[cntryPerMonth['Month'] == 3.0, 'Month'] = 'March'
cntryPerMonth.loc[cntryPerMonth['Month'] == 4.0, 'Month'] = 'April'
cntryPerMonth.loc[cntryPerMonth['Month'] == 5.0, 'Month'] = 'May'
and so on for the remaining months
有没有更有效的方法


谢谢。

您可以使用字典替换以下值:

replace_dict = {
    1:'Jan', 2:'Feb'
}


df = pd.DataFrame({'month': [1,2]})

df['month_names'] = df['month'].replace(replace_dict)