熊猫数据框。列一致性。将整数值定长

时间:2018-08-28 15:40:15

标签: python pandas csv dataframe

我通过以下方式打开.tsv文件:

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)       |
+---+----------+-------------------------------------+

在初始.tsv文件中,影片ID列中的所有值都是固定长度,并以0开头,例如0000008、0000012、0000091、0000417。

我稍后需要将此列与另一个数据框合并,该数据框的数字格式为tt0000008,tt0000012。为此,我尝试完全获取数字,而不忽略0。

拥有0000008、0000012、0000091、0000417之类的整数的方式是什么?

1 个答案:

答案 0 :(得分:1)

我建议转换为str,然后使用padrjust进行格式化

s.astype(str).str.rjust(7,'0')
Out[168]: 
0    0000008
1    0000012
2    0000091
3    0000417
dtype: object