如果列中的值在 pandas 中的列表中,则将值写入新列
Write value to new column if value in column is in a list in pandas
我正在尝试在 pandas.
中使用列表理解来创建一些复杂的列
例如,我正在尝试使用列表作为参考在 pandas 数据框中创建另一列:
fruit = ['watermelon', 'apple', 'grape']
string new_column
watermelons are cool watermelon
apples are good apple
oranges are on sale NaN
我尝试使用列表理解 -
df['new_column'] = [f in fruit if any(f in s for f in fruit) for s in df['string']]
我认为这不正确,需要一些帮助!
最好使用 str.extract
:
fruit = ['watermelon', 'apple', 'grape']
import re
df['new_column'] = df['string'].str.extract(f"({'|'.join(map(re.escape, fruit))})")
输出:
string new_column
0 watermelons are cool watermelon
1 apples are good apple
2 oranges are on sale NaN
这将完成工作:
import pandas as pd
import numpy as np
fruit = ['watermelon', 'apple', 'grape']
df = pd.DataFrame()
df['string'] = ['watermelons are cool', 'apples are good', 'oranges are on sale', 'apples are not watermelons']
output = df['string'].apply(lambda x: ','.join([f for f in fruit if f in x]))
output[output == ''] = np.nan
print(output)
输出:
0 watermelon
1 apple
2 NaN
3 watermelon,apple
Name: string, dtype: object
我正在尝试在 pandas.
中使用列表理解来创建一些复杂的列例如,我正在尝试使用列表作为参考在 pandas 数据框中创建另一列:
fruit = ['watermelon', 'apple', 'grape']
string new_column
watermelons are cool watermelon
apples are good apple
oranges are on sale NaN
我尝试使用列表理解 -
df['new_column'] = [f in fruit if any(f in s for f in fruit) for s in df['string']]
我认为这不正确,需要一些帮助!
最好使用 str.extract
:
fruit = ['watermelon', 'apple', 'grape']
import re
df['new_column'] = df['string'].str.extract(f"({'|'.join(map(re.escape, fruit))})")
输出:
string new_column
0 watermelons are cool watermelon
1 apples are good apple
2 oranges are on sale NaN
这将完成工作:
import pandas as pd
import numpy as np
fruit = ['watermelon', 'apple', 'grape']
df = pd.DataFrame()
df['string'] = ['watermelons are cool', 'apples are good', 'oranges are on sale', 'apples are not watermelons']
output = df['string'].apply(lambda x: ','.join([f for f in fruit if f in x]))
output[output == ''] = np.nan
print(output)
输出:
0 watermelon
1 apple
2 NaN
3 watermelon,apple
Name: string, dtype: object