使用带有字符串的 .apply() Pandas 创建列
Create columns with .apply() Pandas with strings
我有一个数据框 df
。
其中一列名为 Adress
并包含一个字符串。
我创建了一个函数 processing(string)
,它将一个字符串作为参数,return 是该字符串的一部分。
我成功地将函数应用于 df 并在 df
中创建了一个新列:
df.loc[:, 'new_col_name`] = df.loc[:, 'Adress`].apply(processing)
我修改了函数 processing(string)
,使其 return 有两个字符串。我想将第二个字符串 returned 存储在另一个新列中。
为此,我尝试按照以下步骤操作:
这是我的函数的一个例子processing(string)
:
def processing(string):
#some processing
return [A_string, B_string]
我也试过return一个tuple
中的两个字符串。
以下是我尝试将函数应用于我的 df 的不同方法:
df.loc[:, '1st_new_col'], df.loc[:, '2nd_new_col'] = df.loc[:, 'Adress'].apply(processing)
>>> ValueError: too many values to unpack (expected 2)
#or
df.loc[:, '1st_new_col'], df.loc[:, '2nd_new_col'] = df.loc[:, 'Adress'].astype(str).apply(processing)
>>> ValueError: too many values to unpack (expected 2)
#or
df.loc[:, ['1st_new_col', '2nd_new_col']] = df.loc[:, 'Adress'].apply(processing)
>>> KeyError: "None of [Index(['1st_new_col', '2nd_new_col'], dtype='object')] are in the [columns]"
#or
df.loc[:, ['1st_new_col', '2nd_new_col']] = df.loc[:, 'Adress'].apply(processing, axis=1)
>>> TypeError: processing() got an unexpected keyword argument 'axis'
#or
df.loc[:, ['1st_new_col', '2nd_new_col']] = df.apply(lambda x: processing(x['Adress'], axis=1)
>>> KeyError: "None of [Index(['1st_new_col', '2nd_new_col'], dtype='object')] are in the [columns]"
#or
df.loc[:, ['1st_new_col', '2nd_new_col']] = df.apply(lambda x: processing(x['Adress'].astype(str), axis=1)
>>> AttributeError: 'str' object has no attribute 'astype'
#This is the only Error I could understand
#or
df.loc[:, ['1st_new_col', '2nd_new_col']] = df.apply(lambda x: processing(x['Adress'])
>>> KeyError: 'Adress'
我想我已经很接近了,但我不知道如何获得它。
尝试:
df["Adress"].apply(process)
另外,最好在apply
函数中return一个pd.Series
。
举个例子:
# build example dataframe
df = pd.DataFrame(data={'Adress' : ['Word_1_1 Word_1_2','Word_2_1 Word_2_2','Word_3_1 Word_3_2','Word_4_1 Word_4_2']})
print(df)
# Adress
# 0 Word_1_1 Word_1_2
# 1 Word_2_1 Word_2_2
# 2 Word_3_1 Word_3_2
# 3 Word_4_1 Word_4_2
# Define your own function : here return two elements
def process(my_str):
l = my_str.split(" ")
return pd.Series(l)
# Apply the function and store the output in two new columns
df[["new_col_1", "new_col_2"]] = df["Adress"].apply(process)
print(df)
# Adress new_col_1 new_col_2
# 0 Word_1_1 Word_1_2 Word_1_1 Word_1_2
# 1 Word_2_1 Word_2_2 Word_2_1 Word_2_2
# 2 Word_3_1 Word_3_2 Word_3_1 Word_3_2
# 3 Word_4_1 Word_4_2 Word_4_1 Word_4_2
你可以试试这个。
df['new_column'] = df.apply(lambda row: processing(row['Address']), axis=1)
或者这个。
df['new_column'] = df['Address'].apply(lambda value: processing(value))
我有一个数据框 df
。
其中一列名为 Adress
并包含一个字符串。
我创建了一个函数 processing(string)
,它将一个字符串作为参数,return 是该字符串的一部分。
我成功地将函数应用于 df 并在 df
中创建了一个新列:
df.loc[:, 'new_col_name`] = df.loc[:, 'Adress`].apply(processing)
我修改了函数 processing(string)
,使其 return 有两个字符串。我想将第二个字符串 returned 存储在另一个新列中。
为此,我尝试按照以下步骤操作:
这是我的函数的一个例子processing(string)
:
def processing(string):
#some processing
return [A_string, B_string]
我也试过return一个tuple
中的两个字符串。
以下是我尝试将函数应用于我的 df 的不同方法:
df.loc[:, '1st_new_col'], df.loc[:, '2nd_new_col'] = df.loc[:, 'Adress'].apply(processing)
>>> ValueError: too many values to unpack (expected 2)
#or
df.loc[:, '1st_new_col'], df.loc[:, '2nd_new_col'] = df.loc[:, 'Adress'].astype(str).apply(processing)
>>> ValueError: too many values to unpack (expected 2)
#or
df.loc[:, ['1st_new_col', '2nd_new_col']] = df.loc[:, 'Adress'].apply(processing)
>>> KeyError: "None of [Index(['1st_new_col', '2nd_new_col'], dtype='object')] are in the [columns]"
#or
df.loc[:, ['1st_new_col', '2nd_new_col']] = df.loc[:, 'Adress'].apply(processing, axis=1)
>>> TypeError: processing() got an unexpected keyword argument 'axis'
#or
df.loc[:, ['1st_new_col', '2nd_new_col']] = df.apply(lambda x: processing(x['Adress'], axis=1)
>>> KeyError: "None of [Index(['1st_new_col', '2nd_new_col'], dtype='object')] are in the [columns]"
#or
df.loc[:, ['1st_new_col', '2nd_new_col']] = df.apply(lambda x: processing(x['Adress'].astype(str), axis=1)
>>> AttributeError: 'str' object has no attribute 'astype'
#This is the only Error I could understand
#or
df.loc[:, ['1st_new_col', '2nd_new_col']] = df.apply(lambda x: processing(x['Adress'])
>>> KeyError: 'Adress'
我想我已经很接近了,但我不知道如何获得它。
尝试:
df["Adress"].apply(process)
另外,最好在apply
函数中return一个pd.Series
。
举个例子:
# build example dataframe
df = pd.DataFrame(data={'Adress' : ['Word_1_1 Word_1_2','Word_2_1 Word_2_2','Word_3_1 Word_3_2','Word_4_1 Word_4_2']})
print(df)
# Adress
# 0 Word_1_1 Word_1_2
# 1 Word_2_1 Word_2_2
# 2 Word_3_1 Word_3_2
# 3 Word_4_1 Word_4_2
# Define your own function : here return two elements
def process(my_str):
l = my_str.split(" ")
return pd.Series(l)
# Apply the function and store the output in two new columns
df[["new_col_1", "new_col_2"]] = df["Adress"].apply(process)
print(df)
# Adress new_col_1 new_col_2
# 0 Word_1_1 Word_1_2 Word_1_1 Word_1_2
# 1 Word_2_1 Word_2_2 Word_2_1 Word_2_2
# 2 Word_3_1 Word_3_2 Word_3_1 Word_3_2
# 3 Word_4_1 Word_4_2 Word_4_1 Word_4_2
你可以试试这个。
df['new_column'] = df.apply(lambda row: processing(row['Address']), axis=1)
或者这个。
df['new_column'] = df['Address'].apply(lambda value: processing(value))