Issue
I have an inventory system with two separate SKUs in a single string, example (FT3M-FL3M), I want to make a list called "first_item" and append the first 4 letters (first SKU) to it and do this for every item in the column. Then do the same but with taking the last 4 digits of each string (second SKU)
This is the example dataframe
| Index | Skus |
| -------- | -------------- |
| First | FT3M-FL3M |
| Second | FT2M-FL2M |
| Second | FT1M-FB1M |
| Second | FT4S-FL4S |
| Second | FT4S-FB2M |
This is the code I have:
first_list = []
for i in sku:
first_list.append(i[0:4])
print(first_list)
It clearly doesn't work and I get a massive list.
Solution
If you need a list of first / last n
characters of a string inside a pandas column you can try:
# first
my_first = [x[:4] for x in df.wanted_column]
# last
my_last = [x[-4:] for x in df.wanted_column]
This gives you a possibility to filter the list on the fly if needed, for example:
my_filtered = my_first = [x[:4] for x in df.wanted_column if 'some_sku' not in x]
Answered By - Jonas Palačionis Answer Checked By - Willingham (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.