I have a pandas dataframe column called "elements". It either has numbers of length 9 or of length 7 in it's value. The first row of dataframe is always the one with 9 numbers and the last row is always with 7.
Example:
{element:[120000000, 8000000, 120003000, 7000000, 120003333, 120003444, 5000000]
I want to create another column called "assigned". That column has to assign values, going from backwards of the dataframe, taking the 7 number value from column "element" until it meets 9 number value. Then use the 7 number value it used before for the last time and go to a new row to start using next 7 number value.
Here is an example:
{element:[120000000, 8000000, 120003000, 7000000, 120003333, 120003444, 5000000]}
{assigned:[8000000, 8000000, 7000000, 7000000, 5000000, 5000000, 5000000]}
Honestly I am stuck and have no idea how to do it.
CodePudding user response:
Use Series.where
for replace values greater like 100000000
to missing values and then back filling next values, last convert to integers:
df['assigned'] = df['element'].where(df['element'].lt(100000000)).bfill().astype(int)
print (df)
element assigned
0 120000000 8000000
1 8000000 8000000
2 120003000 7000000
3 7000000 7000000
4 120003333 5000000
5 120003444 5000000
6 5000000 5000000
CodePudding user response:
One option, is to use str.len
with bfill
:
#is it a 7-digit number ?
m = df["element"].astype(str).str.len().eq(7)
df["assigned"] = df["element"].where(m).bfill(downcast="infer")
Output:
print(df)
element assigned
0 120000000 8000000
1 8000000 8000000
2 120003000 7000000
3 7000000 7000000
4 120003333 5000000
5 120003444 5000000
6 5000000 5000000