I want to create a new column from an extracting Data Frame (DF) column.
All my testing indicates the values I am using are correct and should produce a level1 value vs NAN.
Help!
CODE SNIPPET:
import pandas as pd
string = df['currentagentsnapshot']
start = string.str.find('agent-group') + 55
stop = string.str.find('}, level2=')
df['start'] = string.str.find('agent-group') + 55
df['stop'] = string.str.find('}, level2=')
df['level1'] = string.str[df['start']:df['stop']]
print(df.head())
SAMPLE OUTPUT OF KEY FIELDS:
awsaccountid start stop level1
0 992974280925 410 414 NaN
1 992974280925 410 414 NaN
2 992974280925 410 414 NaN
3 992974280925 408 412 NaN
4 992974280925 408 412 NaN
Note: df[‘currentagentsnapshot’] is a LARGE text string. As long as start and stop are both numbers — and stop > start — I would expect string.str[df[‘start’]:df[‘stop’]] to produce the expected result.
Running the above script produces NAN instead of the expected string value.
All the examples I have checked on the WEB reference constant vs calculated values.
When I substitute constant for calculated values in string.str[start : stop] it works.
user25137531 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.