Python 添加一列,该列的值可以是多个不同的值

Python 添加一列,该列的值可以是多个不同的值,python,pandas,data-science,Python,Pandas,Data Science,我正在尝试向pandas数据集添加一个新列,该数据集从一列中提取数字月份,并将相应的季节存储到新列中 所以实质上, if month == 12, season = 'Winter' elif month == 2, season = 'Spring' elif month == 5, season = 'Summer' else month == 8, season = 'Fall' 我还没有看到一个真正明确的解决方案。我已经了解了如何使用2个条件值,但我对pandas和python还是相当

我正在尝试向pandas数据集添加一个新列,该数据集从一列中提取数字月份,并将相应的季节存储到新列中

所以实质上,

if month == 12, season = 'Winter'
elif month == 2, season = 'Spring'
elif month == 5, season = 'Summer'
else month == 8, season = 'Fall'
我还没有看到一个真正明确的解决方案。我已经了解了如何使用2个条件值,但我对pandas和python还是相当陌生的


编辑:我可以使用下面列出的解决方案之一(谢谢!),但我应该提到,我还需要包括第1、3、4、6、7、9、10、11个月,请尝试以下内容。公平警告,因为没有给出输入和预期输出的样本,所以没有对其进行测试

import pandas as pd
import numpy as np
conditions=[df['month']==12,
            df['month']==2,
            df['month']==5,
            df['month']==8]
choices=['Winter','spring','Summer','fall']

df['season']=np.select(conditions,choices,default=np.nan)

你能试试下面的吗。公平警告,因为没有给出输入和预期输出的样本,所以没有对其进行测试

import pandas as pd
import numpy as np
conditions=[df['month']==12,
            df['month']==2,
            df['month']==5,
            df['month']==8]
choices=['Winter','spring','Summer','fall']

df['season']=np.select(conditions,choices,default=np.nan)

您可以创建一个自定义函数并使用
.apply(函数名,axis=1)
,也可以将
np.where()
与所有条件一起使用。我将使用
np.where()
仅针对4个条件:

df['season'] = np.where(df['month'] == 12,'Winter',
      np.where(df['month'] == 2,'Spring',
             np.where(df['month'] == 5,'Summer',
                    np.where(df['month'] == 8,'fall',np.nan))))
以下是一个例子:

import pandas as pd
import numpy as np
data = {'month':[2,5,3,11,12]}
df = pd.DataFrame(data)

df['season'] = np.where(df['month'] == 12,'Winter',
      np.where(df['month'] == 2,'Spring',
             np.where(df['month'] == 5,'Summer',
                    np.where(df['month'] == 8,'fall',np.nan))))
print(df)
输出:

   month  season
0      2  Spring
1      5  Summer
2      3     nan
3     11     nan
4     12  Winter

您可以创建一个自定义函数并使用
.apply(函数名,axis=1)
,也可以将
np.where()
与所有条件一起使用。我将使用
np.where()
仅针对4个条件:

df['season'] = np.where(df['month'] == 12,'Winter',
      np.where(df['month'] == 2,'Spring',
             np.where(df['month'] == 5,'Summer',
                    np.where(df['month'] == 8,'fall',np.nan))))
以下是一个例子:

import pandas as pd
import numpy as np
data = {'month':[2,5,3,11,12]}
df = pd.DataFrame(data)

df['season'] = np.where(df['month'] == 12,'Winter',
      np.where(df['month'] == 2,'Spring',
             np.where(df['month'] == 5,'Summer',
                    np.where(df['month'] == 8,'fall',np.nan))))
print(df)
输出:

   month  season
0      2  Spring
1      5  Summer
2      3     nan
3     11     nan
4     12  Winter

您可以创建带有相应编号和季节名称的词典,然后将其映射到新系列:

season_dict = {12: "Winter",
              2: "Spring",
              5: "Summer",
              8: "Fall"}

df["season"] = df.month.replace(season_dict)

您可以创建带有相应编号和季节名称的词典,然后将其映射到新系列:

season_dict = {12: "Winter",
              2: "Spring",
              5: "Summer",
              8: "Fall"}

df["season"] = df.month.replace(season_dict)

因为您已经通过pandas使用了numpy,所以最好的方法是通过np.where命令

df['seasure']=np.其中(df['month']==11 | df['month']==12 | df['month']==1,‘Winter’)
df['seasure']=np.其中(df['month']==2 | df['month']==3 | df['month']==4,‘Spring’)
df['seasure']=np.其中(df['month']==5 | df['month']==6 | df['month']==7,‘Summer’)
df['seasure']=np.其中(df['month']==8 | df['month']==9 | df['month']==10,‘Fall’)

希望这有帮助

因为您已经通过pandas使用了numpy,所以最好的方法是通过np.where命令

season_map = {12: "Winter",
              2: "Spring",
              5: "Summer",
              8: "Fall"} # Add the rest to this dictionary

df['season'] = df['month_num'].apply(lambda x: season_map[x]) 
# 'season' is the new column, replace 'month_num' accordingly
df['seasure']=np.其中(df['month']==11 | df['month']==12 | df['month']==1,‘Winter’)
df['seasure']=np.其中(df['month']==2 | df['month']==3 | df['month']==4,‘Spring’)
df['seasure']=np.其中(df['month']==5 | df['month']==6 | df['month']==7,‘Summer’)
df['seasure']=np.其中(df['month']==8 | df['month']==9 | df['month']==10,‘Fall’)

希望这有帮助

这里有一些野生的解决方案

season_map = {12: "Winter",
              2: "Spring",
              5: "Summer",
              8: "Fall"} # Add the rest to this dictionary

df['season'] = df['month_num'].apply(lambda x: season_map[x]) 
# 'season' is the new column, replace 'month_num' accordingly
这显然不是一个完整的示例,但足以理解您需要做什么:

#月号图->季名
月份_dict={12:'冬季',2:'春季',…}
df[‘季节’]=df[‘月数’]。地图(月数)
是的,就是这样


如果您有任何问题,请告诉我:)

这里有一些疯狂的解决方案

这显然不是一个完整的示例,但足以理解您需要做什么:

#月号图->季名
月份_dict={12:'冬季',2:'春季',…}
df[‘季节’]=df[‘月数’]。地图(月数)
是的,就是这样



如果您有任何问题,请告诉我:)

我假设您有一个包含多个月条目的大型df。如果您使用month-seasure(aka
df.shape=(2,12)
)创建df,那么您可以在month列上进行连接。您做了哪些尝试来解决此问题?你尝试过什么吗?还有,你尝试过哪种解决方案?我相信我的是其中最简单的。我假设你有一个大的df,有很多个月的条目。如果您使用month-seasure(aka
df.shape=(2,12)
)创建df,那么您可以在month列上进行连接。您做了哪些尝试来解决此问题?你尝试过什么吗?还有,你尝试过哪种解决方案?我相信我的是他们中最简单的一个。这确实有效,是的。只需添加每个选项的副本,以对应我没有明确列出的月份。请注意,其中有一个不匹配的引用。@AlexanderCécile,感谢buddy让我知道,我现在修复了它,非常感谢。@Michael,很高兴它帮助了你。找个时间,当你的问题没有答案时,你也可以选择“任何人”作为正确答案,干杯。@Alexander Cécile,嗨Alex,如果你不是被否决的选民,请道歉,如果你已经放弃投票,那么请让我知道同样的原因,谢谢。这确实有效,是的。只需添加每个选项的副本,以对应我没有明确列出的月份。请注意,其中有一个不匹配的引用。@AlexanderCécile,感谢buddy让我知道,我现在修复了它,非常感谢。@Michael,很高兴它帮助了你。有时间,当你对你的问题几乎没有答案时,你也可以选择“任何人”作为正确答案,干杯。@Alexander Cécile,嗨Alex,如果你不是被否决的选民,很抱歉,如果你已经放弃了投票,那么请让我知道同样的原因,谢谢。
df['seasure']=df['month_num'].apply(lambda x:seasure_map[x])
-->
df['seasure']=df['month\u num'].map(seasure\u map)
我不明白为什么我的答案被否决了。随附的关于我的代码哪里出错的解释将是受欢迎和有益的。请参阅我上面的评论。
df['seasure']=df['month\u num'].apply(lambda x:seasure\u map[x])
-->df['seasure']=df['month\u num'].map(seasure\u map)我不明白为什么我的答案被否决了。关于我的代码错在哪里的附带解释将是受欢迎和有益的。请参阅我上面的评论。
numpy.where()
在这里完全没有必要吗?我的意思是,这是一种方法。。。不过,您的方法肯定要简单一些:)根据我有限的经验,我不熟悉.map命令。一般的方法很好,我的意思是
where()