I have a jupyter notebook, containing a pandas dataframe, with a column PAR (dtype = obj).
+------+------------------+
| | PAR |
+------+------------------+
| 0 | [[1.2.3, 2.3.4]] |
+------+------------------+
| 1 | [[3.2, 3.2]] |
+------+------------------+
I do not understand how tyo 'clean' each [[list]] in each row, into something like [list].
I can print row contents:
print(df['PAR'][1])
print(', '.join(df['PAR'][1][0]))
This outputs:
[['3.2', '3.2']]
3.2, 3.2
I can also 'strip' each cell into a string:
# df['PAR'] = df['PAR'].astype(str)
df['PAR'].replace(r'\[','', regex=True, inplace=True)
df['PAR'].replace(r'\]','', regex=True, inplace=True)
df['PAR'].replace(r'\'','', regex=True, inplace=True)
This gives a clean-ish string, although this is not the format that I need:
3.2, 3.2
But, what I'm looking for is a 1-level list in each row of my df, something like this:
+------+------------------+------------------+
| | PAR | PAR list |
+------+------------------+------------------+
| 0 | [[1.2.3, 2.3.4]] | [1.2.3, 2.3.4] |
+------+------------------+------------------+
| 1 | [[3.2, 3.2]] | [3.2, 3.2] |
+------+------------------+------------------+
(the spaces between comma and nth element are just for a better reading of the table above).
What would be a common approach to do this?
My next step is converting each new list into a list with only unique elements, following this thread: Get unique values from a list in python
mylist = ['nowplaying', 'PBS', 'PBS', 'nowplaying', 'job', 'debate', 'thenandnow']
myset = set(mylist)
mynewlist = list(myset)
So I'd appreciate some help to 'unlist' the lists in each row. A solution with a lambda-function (.map of .join?) would be easy for me to handle.