从 pandas 列表系列中提取元素并存储为单独的系列
Extract element from pandas list-series and store as separate series
我有这个 df(带有样本期望结果)
dfn = pd.DataFrame({"country_code": ["USA, UK, FRA", "RUS, ZHC, JAP", "IN, BRA, ES"],
"all_but_american_desired": [["United Kingdom", "France"], ["Russia", "China", "Japan"], ["India", "Spain"]]})
我将(到目前为止)字符串“翻译”成新的含义并存储为元素列表
masked = {"USA":"United States", "UK":"United Kingdom", "FRA":"France",
"RUS":"Russia", "ZHC":"China", "JAP":"Japan",
"IN":"India", "BRA":"Brazil", "ES":"Spain"}
dfn["country_name"] = dfn["country_code"].apply(lambda x: [", ".join({masked[i] for i in x.split(", ")})])
然后我想提取一些由外部列表翻译的 country_name 系列,american
并将它们放在一个单独的系列中 (all_but_american
)
american = ["United States", "Brazil"]
结果应与 all_but_american_desired
系列相同。到目前为止我尝试了什么:
dfn["all_but_american1"] = dfn["country_name"].apply(lambda x: [i for i in x if i not in american])
我之前用过 attempt1 完全相同的方法并且它有效,但是这次没有任何效果而且我找不到它的原因(这次我也尝试了其他方法,但是因为我不熟悉他们我会避免发布)......有人可以检查一下吗?如果可能的话,也解释一下我做错了什么。
对于 country_name
创建列表而不是一个具有连接值的元素列表:
dfn["country_name"] = dfn["country_code"].apply(lambda x: [masked[i] for i in x.split(", ")])
然后你的第二个解决方案运行良好:
american = ["United States", "Brazil"]
dfn["all_but_american1"] = dfn["country_name"].apply(lambda x: [i for i in x if i not in american])
print (dfn)
country_code all_but_american_desired \
0 USA, UK, FRA [United Kingdom, France]
1 RUS, ZHC, JAP [Russia, China, Japan]
2 IN, BRA, ES [India, Spain]
country_name all_but_american1
0 [United States, United Kingdom, France] [United Kingdom, France]
1 [Russia, China, Japan] [Russia, China, Japan]
2 [India, Brazil, Spain] [India, Spain]
我有这个 df(带有样本期望结果)
dfn = pd.DataFrame({"country_code": ["USA, UK, FRA", "RUS, ZHC, JAP", "IN, BRA, ES"],
"all_but_american_desired": [["United Kingdom", "France"], ["Russia", "China", "Japan"], ["India", "Spain"]]})
我将(到目前为止)字符串“翻译”成新的含义并存储为元素列表
masked = {"USA":"United States", "UK":"United Kingdom", "FRA":"France",
"RUS":"Russia", "ZHC":"China", "JAP":"Japan",
"IN":"India", "BRA":"Brazil", "ES":"Spain"}
dfn["country_name"] = dfn["country_code"].apply(lambda x: [", ".join({masked[i] for i in x.split(", ")})])
然后我想提取一些由外部列表翻译的 country_name 系列,american
并将它们放在一个单独的系列中 (all_but_american
)
american = ["United States", "Brazil"]
结果应与 all_but_american_desired
系列相同。到目前为止我尝试了什么:
dfn["all_but_american1"] = dfn["country_name"].apply(lambda x: [i for i in x if i not in american])
我之前用过 attempt1 完全相同的方法并且它有效,但是这次没有任何效果而且我找不到它的原因(这次我也尝试了其他方法,但是因为我不熟悉他们我会避免发布)......有人可以检查一下吗?如果可能的话,也解释一下我做错了什么。
对于 country_name
创建列表而不是一个具有连接值的元素列表:
dfn["country_name"] = dfn["country_code"].apply(lambda x: [masked[i] for i in x.split(", ")])
然后你的第二个解决方案运行良好:
american = ["United States", "Brazil"]
dfn["all_but_american1"] = dfn["country_name"].apply(lambda x: [i for i in x if i not in american])
print (dfn)
country_code all_but_american_desired \
0 USA, UK, FRA [United Kingdom, France]
1 RUS, ZHC, JAP [Russia, China, Japan]
2 IN, BRA, ES [India, Spain]
country_name all_but_american1
0 [United States, United Kingdom, France] [United Kingdom, France]
1 [Russia, China, Japan] [Russia, China, Japan]
2 [India, Brazil, Spain] [India, Spain]