Reputation: 65
I have a DataFrame like the following
A B C
0 5 4) 0 3 2)
1 1 9 1
2 0,10 3) 9 0,22 3)
3 0.5 10 0.2
4 0,07 2) 2 0,05 3)
Now i want to change the values, that contain a bracket ")" so that only the first number remains. Like this:
A B C
0 5 0 3
1 1 9 1
2 0,10 9 0,22
3 0.5 10 0.2
4 0,07 2 0,05
I tried it with the following code, which was sucessful.
for i in df["A"]:
if ")" in str(i):
new, filler = str(i).split(" ",1)
df.replace(i, new,inplace=True)
Due to the large quantity of data, i need to do this operation on the whole DataFrame and not only on column "A". What is the best way to this? Thanks!
Upvotes: 1
Views: 45
Reputation: 88236
One approach is to split on the spaces and keep the first element of the resulting lists:
cols = ['A', 'C']
df[cols] = df[cols].apply(lambda x: x.str.split(' ').str[0])
print(df)
A B C
0 5 0 3
1 1 9 1
2 0,10 9 0,22
3 0.5 10 0.2
4 0,07 2 0,05
Upvotes: 3