I have a coding question. Can someone help?

Hello I have a coding question. So I have a dataset that basically have Tract IDs, and I know they should have 6 digits but since some start with 0, some have less than 6 digits, and before I want to add the state and county identifier, I want to make sure each tract ID has 6 digits, like if it has 5 digits, I want there to be a 0 at the beginning, and so on. Is there a way for me to code that, because Im hoping to match CBGs to their CTs which have populations I will have to use for my research, thank you.


This topic was automatically generated from Slack. You can find the original thread here.

What language are you using?

Im using pandas library in python

you could also make sure you’re reading in the column as a string from the beginning. I am guessing the original file has the leading 0s there but Pandas is interpreting it as an integer

Probably, yeah. I just want to make sure each ct has 10 digits

And zfill helps add 0s to the front of the string?

yep. so if you have 123 and you want it to be 000123, you would do zfill(6), where 6 is the length you want for the resulting string.

That helps me alot thank you!

Thanks again @Eugene_Chong_SafeGraph !

To prevent any further questions from being overlooked, I’ll go ahead and close this thread out. If you have any more questions or follow-up questions, we’re always here to help! Just be sure to make a new post to help, as we aren’t monitoring old threads at this time. Thanks!