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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế