Reputation: 227
I have a few pandas dataframe that consist of 1 column and 5 rows, each dataframe contains 3 values, "1", "0", and "P". These data frames are of type "object".
I am running into problems when i am adding data frames together. il give you an example:
df1 = [ 1, P, 0, 1, 1]
df2 = [1, P, 1, 1,1]
df1 + df2 = [11, PP, 01, 11,11]
Note: A "P" will alway be in the same location in all data frames. so when adding together data frames i could get "PPPPP" for example.
In the example above i am trying to get the output to be:
df1 + df2 = [2, P, 1, 2, 2]
What i am thinking is to use pandas to make each dataframe column numeric and then i should be able to add them together.
pd.to_numeric(df1["my df title"], errors='coerce')
the problem with this is that the "P" values cannot be converted to numeric. how to i make everything numeric except the p values?
what my real dataframe looks like when I add df1 + df2 + df3 + df4:
0 1111
1 PPPP
2 1111
3 1111
4 PPPP
Upvotes: 0
Views: 68
Reputation: 30032
Just convert each value to expected output with if-else
.
import pandas as pd
df1 = pd.DataFrame({'col': [ '1', 'P', '0', '1', '1']})
df2 = pd.DataFrame({'col': [ '1', 'P', '1', '1', '1']})
s = df1['col'] + df2['col']
s = s.map(lambda x: 'P' if 'P' in x else sum(map(int, list(x))))
print(s)
0 2
1 P
2 1
3 2
4 2
Name: col, dtype: object
Upvotes: 2