Assuming I have this dataframe:
X Y
0 a 1
1 b 10
2 c 11
3 d 100
4 e 101
5 f 110
6 g 111
I would like to decompose the column Y into rows so that each number with more than one digit 1 is broken into another number with only one digit 1. For example, the number 111, is broken to 3 rows with values 100, 10, and 1, and keeps the information from other columns. Here is a visualization of what I expect:
X Y
0 a 1
1 b 10
2 c 10
3 c 1
4 d 100
5 e 100
6 e 1
7 f 100
8 f 10
9 g 100
10 g 10
11 g 1
Here is what I have done so far, but I wonder if there is a more pythonic way to do it. Appreciate your help in advance.
import pandas as pd
df = pd.DataFrame({ 'X':['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'], 'Y':[1,10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,1111] })
print(df)
for i in range(len(df)):
value = int(df.at[i,'Y'])
digits = len(str(value))
opts = sum(map(int, str(value)))
if opts > 1:
# assign first value
temp = df.loc[[i]]
temp.at[i,'Y'] = 10**(digits-1)
# update row
df = df.drop([i])
df = df.append(temp)
# append new rows
while value != 1:
value = int(str(value)[1:])
if value == 0: break
digits = len(str(value))
temp.at[i,'Y'] = 10**(digits-1)
df = df.append(temp)
df = df.sort_index()
df = df.reset_index(drop=True)
print(df)