I want to clean the following Pandas dataframe column, but in a single and efficient statement than the way I am trying to achieve it in the code below.
Input:
string
0 ['string', '#string']
1 ['#string']
2 []
Output:
string
0 string, #string
1 #string
2 NaN
Code:
import pandas as pd
import numpy as np
d = {"string": ["['string', '#string']", "['#string']", "[]"]}
df = pd.DataFrame(d)
df['string'] = df['string'].astype(str).str.strip('[]')
df['string'] = df['string'].replace("\'", "", regex=True)
df['string'] = df['string'].replace(r'^\s*$', np.nan, regex=True)
print(df)