Pandas data frame. Column consistency. Bring integer values to fixed length
Pandas data frame. Column consistency. Bring integer values to fixed length
I open the .tsv file in a following way:
cols = ['movie id','movie title','genre']
movies = pd.read_csv('movies.dat', sep='::', index_col=False, names=cols, encoding="UTF-8",)
+---+----------+-------------------------------------+
| | movie id | movie title |
+---+----------+-------------------------------------+
| 0 | 8 | La sortie des usines Lumière (1895) |
| 1 | 12 | The Arrival of a Train (1896) |
| 2 | 91 | Le manoir du diable (1896) |
| 3 | 417 | Le voyage dans la lune (1902) |
+---+----------+-------------------------------------+
In the initial .tsv file all the values in movie id column are fixed length and start with 0, for example 0000008, 0000012, 0000091, 0000417.
I need to merge this column later with another data frame, that has numbers in the format tt0000008, tt0000012. For this I try to get the numbers fully, without omitting 0.
What would be the way to have full numbers like 0000008, 0000012, 0000091, 0000417?
1 Answer
1
I will recommend convert to str
, then format with pad
or rjust
str
pad
rjust
s.astype(str).str.rjust(7,'0')
Out[168]:
0 0000008
1 0000012
2 0000091
3 0000417
dtype: object
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.