I am trying to replace []
values with ''
, strip the spaces, and return a clean Dataframe
Distance TGR Grade TGR1
0 [342m, 342m, 530m, 342m] [M, M, RW, RW] [1, 1, 7, 1]
1 [390m, 390m, 390m, 390m,450] [M, 7, 6G, X45, X67] [1, 2, 4, 5, 5]
I have applied several functions but the Dataframe is either returns nan value or it returns the same Dataframe
To from both sides of the values
df[df.columns]=df[df.columns].apply(lambda x:x.str.strip())
df[cols]=df[cols].astype(str).agg(lambda x:x.str.strip("frozenset({''})"),1)
df.replace('\[', '', regex=True)
df.replace('\]', '', regex=True)
but the df still remains the same
Does this answer your question?
df.columns = [''.join(i.split()) for i in df.columns]
df.applymap(lambda x: ''.join(x.strip('\[').strip('\]').split()))
Output:
Distance TGRGrade TGR1
0 342m,342m,530m,342m M,M,RW,RW 1,1,7,1
1 390m,390m,390m,390m,450 M,7,6G,X45,X67 1,2,4,5,5
Updated with full code:
from io import StringIO
import pandas as pd
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity='all'
d = '''
"Distance","TGR Grade","TGR1"
"[342m, 342m, 530m, 342m]","[M, M, RW, RW]","[1, 1, 7, 1]"
"[390m, 390m, 390m, 390m,450]","[M, 7, 6G, X45, X67]","[1, 2, 4, 5, 5]"
'''
df = pd.read_csv(StringIO(d))
df
df.columns = [''.join(i.split()) for i in df.columns]
df = df.applymap(lambda x: ''.join(x.strip('\[').strip('\]').split()))
df
Output:
Distance TGR Grade TGR1
0 [342m, 342m, 530m, 342m] [M, M, RW, RW] [1, 1, 7, 1]
1 [390m, 390m, 390m, 390m,450] [M, 7, 6G, X45, X67] [1, 2, 4, 5, 5]
Distance TGRGrade TGR1
0 342m,342m,530m,342m M,M,RW,RW 1,1,7,1
1 390m,390m,390m,390m,450 M,7,6G,X45,X67 1,2,4,5,5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.