我有两个数据框:
d = {'year': [1990, 1991], 'org': ['EU', 'EU'], 'UK': [1, 1], 'Croatia': [-9, 1], 'Germany': [1,1]}
df1 = pd.DataFrame(data=d)
df1
year org UK Croatia Germany
0 1990 EU 1 -9 1
1 1991 EU 1 1 1
d = {'year': [1990, 1991], 'country1': ['Israel', 'EU'], 'country2': ['EU', 'Brzail']}
df2 = pd.DataFrame(data=d)
df2
year country1 country2
0 1990 Israel EU
1 1991 EU Brzail
我想将df2中的欧盟更改为df1中具有1的国家。 结果应该像这样:
year country1 country2
0 1990 Israel UK, Germany
1 1991 UK, Germany, Croatia Brzail
实现此目标的最佳方法是什么?
答案 0 :(得分:1)
让我们先尝试dot
然后尝试merge
s = df1.loc[:,'UK':]
s.eq(1).dot(s.columns+',').str[:-1]
df1['New'] = s.eq(1).dot(s.columns+',').str[:-1]
df2 = df2.merge(df1[['year','New']])
newdf2 = df2.mask(df2=='EU',df2.New,axis=0).drop('New',1)
newdf2
Out[249]:
year country1 country2
0 1990 Israel UK,Germany
1 1991 UK,Croatia,Germany Brzail
答案 1 :(得分:0)
首先,我为这个麻烦的解决方案道歉,但这是我能想到的最好的解决方法。
df3 = df1.melt(id_vars = ['year', 'org'])
df3 = df3[df3['value'] == 1]
df3 = df3.groupby(['year', 'org'])['variable'].apply(', '.join).reset_index()
df2 = pd.merge(df2, df3, on = 'year', how = 'left')
df2.loc[df2['country1'] == 'EU', 'country1'] = df2['variable']
df2.loc[df2['country2'] == 'EU', 'country2'] = df2['variable']
df2 = df2[['year', 'country1', 'country2']]
答案 2 :(得分:0)
在这里,我正在编写一个自定义函数以获取关键字位置列表(在您的情况下为“ EU”)。
首先,我们将找出特定列索引中带有1的所有国家/地区。我会得到一个数组。
一旦我获得了所有位置(行值,列名),我就使用信息data_frame [列名] [行值]并将其替换为数组索引。
+------------------------------+
| products_with_names |
+----------+---------+---------+
| id | sku | name_en | name_it |
+----+-----+---------+---------+
| 1 | 123 | paper | carta |
| 2 | 456 | rock | sasso |
| 3 | 789 | scissor | forbice |
+----+-----+---------+---------+