Let's say I have a dataframe that looks like this:
REFERENCE_CODE
dog
1
2
3
4
cat
1
2
4
5
rat
3
4
5
fish
4
5
6
Notice the spaces.. I would like to achieve a dataframe that looks like this:
REFERENCE_CODE
dog
dog_1
dog_2
dog_3
dog_4
cat
cat_1
cat_2
cat_4
cat_5
rat
rat_3
rat_4
rat_5
fish
fish_4
fish_5
fish_6
I have tried something similar to the following:
for index, row in df.iterrows():
if isinstance(row['REFERENCE_CODE'], str):
great! continue
elif isinstance(row['REFERENCE_CODE'], int):
go back up and find the last instance, concatenate
else:
pass
I am having trouble filling out the areas where there is pseudocode. Am I correct in my logic? Is there any easier way to go about doing this? I would ideally like to hold the integrity of the original data in terms of blank spaces, size, etc. but if not, that is ok too. I will find a workaround! Thanks.
As per Andy Hayden:
Traceback (most recent call last):
Question number REFERENCE_CODE ... Unnamed: 12 Unnamed: 13
File "/Users/xxx/Projects/trend_env/src/script4.py", line 10, in <module>
0 Q1a ladder_now ... NaN NaN
1 NaN NaN ... NaN NaN
2 NaN 1 ... NaN NaN
headers = (df.REFERENCE_CODE != '') & ~df.REFERENCE_CODE.str.isnumeric()
3 NaN 2 ... NaN NaN
File "/Users/xxx/Projects/trend_env/lib/python3.7/site-packages/pandas/core/generic.py", line 1466, in __invert__
4 NaN 3 ... NaN NaN
arr = operator.inv(com.values_from_object(self))
TypeError: bad operand type for unary ~: 'float'
Question number REFERENCE_CODE ... Unnamed: 12 Unnamed: 13
0 Q1a ladder_now ... NaN NaN
1 NaN NaN ... NaN NaN
2 NaN 1 ... NaN NaN
3 NaN 2 ... NaN NaN
4 NaN 3 ... NaN NaN
[5 rows x 14 columns]
Traceback (most recent call last):
File "/Users/mitchell_bregman/Projects/trend_env/src/script4.py", line 14, in <module>
headers = (df.REFERENCE_CODE != '') & ~df.REFERENCE_CODE.str.isnumeric()
File "/Users/mitchell_bregman/Projects/trend_env/lib/python3.7/site-packages/pandas/core/generic.py", line 1466, in __invert__
arr = operator.inv(com.values_from_object(self))
TypeError: bad operand type for unary ~: 'float'