2

I have a dataframe like the following:

Transaction No. Alloted Quantity    Rate    Amount  Docket
0   2521    15  1072    16080   https://xxxx/ALT_2521.JPG
1   2524    15  1072    16080   https://xxxx/ALT_2524.JPG
2   2526    15  1072    16080   https://xxxx/ALT_2526.JPG
3   2532    15  1072    16080   https://xxxx/ALT_2532.JPG
4   2538    16  1072    17152   https://xxxx/ALT_2538.JPG
5   2547    12  1020    12240   https://xxxx/ALT_2547.JPG
6   2552    15  1072    16080   https://xxxx/ALT_2552.JPG
7   2554    10  1072    10720   https://xxxx/ALT_2554.JPG
8   2558    15  1020    15300   ['https://xxxx/ALT_2558.JPG', 'https://xxxx/AL...
9   2560    14  1020    14280   ['https://xxxx/ALT_2560.JPG', 'https://xxxx/AL...
10  2564    15  1072    16080   ['https://xxxx/ALT_2564.JPG']
11  2570    15  1020    15300   ['https://xxxx/ALT_2570.JPG']
12  2574    15  1020    15300   ['https://xxxx/ALT_2574.JPG']

where in the Docket column there are some URLs, this column can contain the direct URL or list of URLs or list of URL. How do I destring the URLs and show them without the list?

Expected Output

Transaction No. Alloted Quantity    Rate    Amount  Docket
0   2521    15  1072    16080   https://xxxx/ALT_2521.JPG
1   2524    15  1072    16080   https://xxxx/ALT_2524.JPG
2   2526    15  1072    16080   https://xxxx/ALT_2526.JPG
3   2532    15  1072    16080   https://xxxx/ALT_2532.JPG
4   2538    16  1072    17152   https://xxxx/ALT_2538.JPG
5   2547    12  1020    12240   https://xxxx/ALT_2547.JPG
6   2552    15  1072    16080   https://xxxx/ALT_2552.JPG
7   2554    10  1072    10720   https://xxxx/ALT_2554.JPG
8   2558    15  1020    15300   https://xxxx/ALT_2558.JPG, https://xxxx/ALT_2...
9   2560    14  1020    14280   https://xxxx/ALT_2560.JPG, https://xxxx/ALT_35...
10  2564    15  1072    16080   https://xxxx/ALT_2564.JPG
11  2570    15  1020    15300   https://xxxx/ALT_2570.JPG
12  2574    15  1020    15300   https://xxxx/ALT_2574.JPG
Rahul Sharma
  • 2,187
  • 6
  • 31
  • 76
  • Use `df = df.explode('Docket')` – jezrael Mar 17 '21 at 07:10
  • @jezrael I tried explode but I need the URls in the same row, I have updated the question with expected output, please have a look – Rahul Sharma Mar 17 '21 at 07:17
  • The only way python/pandas is going to store a list of string inside one single cell (without doing explode/concat) is as a list of string (unless you declare some custom object, and there's not point in doing that). You can achieve what you want by only changing the pandas [`.to_string()` method](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_string.html) or `.to_html()` on that dataframe column, not its actual contents. Or else define a custom formatter. – smci Mar 17 '21 at 07:37

1 Answers1

1

You can join only list in lambda function like:

df['Docket'] = df['Docket'].apply(lambda x: ', '.join(x) if isinstance(x, list) else x)

If there are all strings use Series.str.strip with Series.str.replace:

df['Docket'] = df['Docket'].str.strip('[]').str.replace("', '", ", ")

Or:

df['Docket'] = df['Docket'].replace(['\[', '\]',"'"], '', regex=True)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252