I have a pandas dataframe which has four columns. Following is an example of the pandas dataframe:
import pandas as pd
data = {"Name" : ['A1', 'A1', 'A1', 'A1'], "String1" : ["B1", "B2", "B6", "B7"] , "Values1" : [5, 12, 21, 99], "Values2" : [50, 120, 210, 990] }
df = pd.DataFrame(data)
print( df )
Name String1 Values1 Values2
0 A1 B1 5 50
1 A1 B2 12 120
2 A1 B6 21 210
3 A1 B7 99 990
One of the columns, i.e. Name has constant entries only, while two other columns, Values1 and Values2 have numerical values.
I have a list (say String2) which contains all elements of the column String1 and some additional elements.
An example of String2 is the following:
String2 = [ "B1", "B2", "B3", "B4" , "B5", "B6", "B7" ]
I want to find insert all elements which are in String2 and not in String1 (i.e. "B3", "B4" , "B5") in the column String1 in the Pandas dataframe in separate rows. For all these rows where new elements have been inserted in String1, I want to put Null in columns Value1 and Value2. In the constant column (Name), I want to keep the same constant entry (i.e. A1).
In other words, following is how I want the new dataframe to be like:
Name String1 Values1 Values2
0 A1 B1 5 50
1 A1 B2 12 120
2 A1 B3 Null Null
3 A1 B4 Null Null
4 A1 B5 Null Null
5 A1 B6 21 210
6 A1 B7 99 990
How can I do this using python and pandas?