Reputation: 31
I have a dataset in Dataframe like:
A B
a ,FL,GA,KL
b CA
c ,KS
some of the values in column B start with a comma. How I can remove first comma of those cells which are started with a comma. My code is in python.
Thanks
Upvotes: 1
Views: 2478
Reputation: 1168
You can replace
with a regex:
>>> df['B'].replace({'^,':''}, regex=True)
0 FL,GA,KL
1 CA
2 KS
Name: B, dtype: object
>>>
Upvotes: 0
Reputation: 221
We can't really see your table, but I can see your problem.
To remove the first character of a string(for this example, it's stored in a variable called cell
) IF it is a semicolon(or comma? It's in the var first_letter
), use this:
cell[1:] if cell[0] == first_letter else cell
This returns the value you want for that cell. You can loop over all your cells if you want.
Upvotes: 1
Reputation: 456
# Open data and put each line into data[]
with open("mydatafile.txt") as datafile:
data = datafile.read().split("\n")
newdata = []
for line in data:
if line[0] == ",": # Remove comma or whatever if its there
line = line[1:]
newdata.append(line)
# Write new data to file
with open("mynewdatafile.txt","w+") as newdatafile:
for line in newdata:
newdatafile.write(line+"\n")
That should work
Upvotes: 1