I try to sum up columns with string data. The Problem is that I want to ignore the NaN, but I didn't find a solution.
The Dataframe look like this:
s=pd.DataFrame({'A':['(Text,','(Text1,'],'B':['(Text2,','(Text3,'],'C':['(Text4,','(Text5,']})
A B C
0 (Text, (Text2, (Text4,
1 (Text1, (Text3, (Text5,
First I delete the brackets and commas with:
sA = s['A'].str.lstrip('(').str.rstrip(',')
sB = s['B'].str.lstrip('(').str.rstrip(',')
sC = s['C'].str.lstrip('(').str.rstrip(',')
And then I put the columns together.
sNew = sA + ' ' + sB + ' ' + sC
print sNew
0 Text Text2 Text4
1 Text1 Text3 Text5
1.
Is there a better way to sum up the columns? I have the feeling that this way isn't really efficient.
I tried the str.lstrip
for all columns but it doesn't work.
2. If I have a NaN in a Cell, the row will be NaN. How can I ignore the NaN in this spezific case? e.g.
A B C
0 (Text, (Text2, (Text4,
1 (Text1, (Text3, NaN
and my result is after delete the brackets and sum up...
0 Text Text2 Text4
1 NaN
but I want the following result...
0 Text Text2 Text4
1 Text1 Text3
It will be great if you have some tips for me to solve the problem!