str attribute contains most of the methods in string.
df['ID'] = df['ID'].str.zfill(15)
See more: http://pandas.pydata.org/pandas-docs/stable/text.html
Answer from Guangyang Li on Stack Overflowstr attribute contains most of the methods in string.
df['ID'] = df['ID'].str.zfill(15)
See more: http://pandas.pydata.org/pandas-docs/stable/text.html
Try:
df['ID'] = df['ID'].apply(lambda x: '{0:0>15}'.format(x))
or even
df['ID'] = df['ID'].apply(lambda x: x.zfill(15))
EDIT: Solved! Well actually there wasn't really anything to solve lol I'm just a noob and a half. To those interested in the answer, please see the reply by u/spez_edits_thedonald. Thank you to everyone who helped. Wow i love this sub so much
I have a gigantic pandas dataframe (8 million rows) and I need to upload it's contents to our platform at work. Side note: I'm kind of excited because no one in my office has ever done this on this scale, so I'm hoping I can impress my bosses.
Unfortunately, there is a file size limit of 2 MB per upload into our platform. I can't use an API, which means I don't have the option of doing this row by row directly from the pandas dataframe itself (wish I could, that would make this a lot easier). This also means that I'm not really able to manipulate the data using pandas between file upload iterations... at least I dont think so? My plan is to slice my gigantic dataframe up into a bunch of smaller CSV files (I'm pretty sure I can figure out how to do this part myself) and then do some browser automation with selenium. Selenium sounds agonizing but honestly I'm cool with letting this run all night and then waking up tomorrow to see the finished product.
The problem is: when I save it to a CSV, the leading zeros in one of my columns get dropped. This is stopping me dead in my tracks. I can't figure out how to preserve leading zeros in the CSV itself. I know how to add leading zeros in a pandas df by doing:
df['my_column'] = df['my_column'].apply(lambda x: x.zfill(5)) but this doesn't help me once it's saved to the CSV
Here is an example of my dataframe:
| color | shape | identifier | |
|---|---|---|---|
| 0 | blue | circle | 06432 |
| 1 | red | square | 01245 |
| 2 | green | triangle | 08750 |
| 3 | yellow | oval | 12350 |
| 4 | orange | rectangle | 19862 |
This data is good and ready to go but when I save it to a csv by doing df.to_csv('saved_df.csv') it becomes this:
| color | shape | identifier | |
|---|---|---|---|
| 0 | blue | circle | 6432 |
| 1 | red | square | 1245 |
| 2 | green | triangle | 8750 |
| 3 | yellow | oval | 12350 |
| 4 | orange | rectangle | 19862 |
Please note how the leading zeros in df['identifier'] are dropped
Does anyone know how to keep leading zeros within the CSV file itself? Ultimately I need the leading zeros to be there after i do df.to_csv('saved_df.csv') and when I'm uploading the CSV to our platform.
I tried doing df['identifier'] = df['identifier'].astype(str) and then saving it to a csv but it seems like that column gets converted back to integers once it is saved to the csv.
I feel like I'm so close to impressing my bosses with this sheer amount of productivity. Any help would be greatly appreciated. Thank you, kind pythonistas of reddit :)