您可以使用apply() https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html为了那个原因:
df1['type_string'] = df2.apply(lambda x: 'Fruit' if x.Fruit else 'Flower', 1)
这是一个运行示例:
import pandas as pd
from io import StringIO
df1 = pd.read_csv(StringIO(
"""
0 A 21 rose
1 B 22 apple
2 C 30 grapes
4 D 21 lily
"""), sep='\s+', header=None)
df2 = pd.read_csv(StringIO(
"""
orange 1 0
apple 1 0
rose 0 1
lily 0 1
grapes 1 0
"""), sep='\s+', header=None)
df1.columns = ['id', 'name', 'age', 'likes']
df2.columns = ['category', 'Fruit', 'Flower']
df1['category'] = df2.apply(lambda x: 'Fruit' if x.Fruit else 'Flower', 1)
Input
id name age likes
0 0 A 21 rose
1 1 B 22 apple
2 2 C 30 grapes
3 4 D 21 lily
Output
id name age likes category
0 0 A 21 rose Fruit
1 1 B 22 apple Fruit
2 2 C 30 grapes Flower
3 4 D 21 lily Flower