Using Pandas, I have a data frame with a column containing a string that I am splitting when a ; or , is seen:
import re
re.split(';|,',x)
I want to iterate through the column in the whole data frame and create a copy of the current data frame with the new splits.
This is what I was trying based off of other answers here.
for row in x:
if pd.notnull(x):
SplitIDs = re.split(';|,',x)
df.iloc[0, df.columns.get_loc('x')] = SplitIDs
I don't know how to access the particular cell that the "for loop" is currently looking at in order to change it to the split format (for the new copy of the data frame).
If I could also get instruction on how to save these changes into a new copy of the data frame, that would be great.
I apologize if my question is not clear. I am very new to scripting in general - the more detailed your explanation is, the better. Thanks!
Alternatively, what if I wanted to create new columns every time the string is split? For example, let's say the string was split into 3 parts now - instead of having the 3 strings under the same existing column, I would like the 2 new pieces are placed into new, adjacent columns.
If we went with this route, if the next row (in the same column) could split into 2 (based on the same parameters we started with), it would take up space of the existing column plus one of the new columns that we just created (and the 3rd would be blank). OR if this row had MORE than the columns we just made (and all the pieces couldn't fit), how do I keep making new columns to fit the pieces?