我正在尝试根据字符串的长度创建一个新列,并在字符串的末端添加白色空间。
data={ 'Fruit':['Apple','Mango','Watermelon'],
'Color':['Red','Yellow','Green']
}
df = pd.DataFrame(data)
df['length']=df['Fruit'].str.len()
df['Fruit_color']=df.apply(lambda row: row['Fruit']+ (' '* row[length])+row['color'])
我得到错误 “TypeError: string indices must be integers”?
当我仅将代码更改为此
时df['white_space']=df.apply(lambda row: (' '* row[length]))
i get KeyError: ('length', 'occurred at index Fruit')
如何避免这些错误以获取所需的结果
问候,Ren。
我们不需要使用apply
df['Fruit']+ df['lenght'].map(lambda x : ' '*x)+df['Color']
Out[689]:
0 Apple Red
1 Mango Yellow
2 Watermelon Green
dtype: object
修复您的代码:1类型,2添加''
df.apply(lambda row: row['Fruit']+ (' '* row['lenght'])+row['Color'],axis=1)
如果您真的想使用apply
:
df['Fruit_color'] = df.apply(lambda x: x['Fruit'] + ' '*x['length'] + x['Color'], axis=1)
^^ ^^
请小心引用列名,否则Python会认为您称为变量。还要谨慎拼写您的变量名称,您同时使用了length
和lenght
理解
zip(*map(df.get, df))
是进行df.itertuples(index=False)
[f'{f}{" " * l}{c}' for f, c, l in zip(*map(df.get, df))]
['Apple Red', 'Mango Yellow', 'Watermelon Green']
作为新列
df.assign(New=[f'{f}{" " * l}{c}' for f, c, l in zip(*map(df.get, df))])
Fruit Color length New
0 Apple Red 5 Apple Red
1 Mango Yellow 5 Mango Yellow
2 Watermelon Green 10 Watermelon Green
无需分配length
df.assign(New=[f'{f}{" " * len(f)}{c}' for f, c in zip(df.Fruit, df.Color)])
Fruit Color New
0 Apple Red Apple Red
1 Mango Yellow Mango Yellow
2 Watermelon Green Watermelon Green