我在dataframe列中有一个文本项列表,其中一些末尾包含整数,而某些则包含在方括号“(extra info)”之间的信息。其余项目只是平面文字。我想从所有具有整数的整数中删除所有整数,并删除其中包含其信息的所有括号,同时仍然保留其位于其后的值。
Cost Item Purchased Name
Store1 22.5 Sponge Chris
Shop 2.5 Kitty Litter Kevyn
House (aax) 2 Spoon Filip
我希望输出为
Cost Item Purchased Name
Store 22.5 Sponge Chris
Shop 2.5 Kitty Litter Kevyn
House 2 Spoon Filip
答案 0 :(得分:0)
df = pd.DataFrame(
{
"cost": [22.5, 2.5, 2],
"item purchased": ["Sponge", "kitty litter", "spoon"],
"name": ["Chris", "Kevyn", "Filip"],
},
index=["Store1", "Shop", "House (aax)"],
)
# reset the index to a column.
df=df.reset_index()
# split the index and keep the first item in the lists.
df['index'] = df['index'].str.split("(").map(lambda x: x[0])
# reset the index
df = df.set_index('index')
print(df)
cost item purchased name
index
Store1 22.5 Sponge Chris
Shop 2.5 kitty litter Kevyn
House 2.0 spoon Filip