按照字符串格式打印熊貓數據框列

用戶785099

我有一個代碼來生成以下輸出

x1 = ['A', 'B', 'C', 'D']
x2 = [',', ',', ',', ',']
test_dataframe = pd.DataFrame()
test_dataframe['id'] = x1
test_dataframe['sep'] = x2
print(test_dataframe[['id','sep']].to_string(index=False))


id sep
 A   ,
 B   ,
 C   ,
 D   ,

但是,我想讓 id 列的輸出遵循以下格式

id  sep
 'A' ,
 'B' ,
 'C' ,
 'D' ,

如何達到這樣的結果。

哈利繪圖儀

另一種選擇是使用Series.map+str.format

test_dataframe['id'] = test_dataframe['id'].map("'{0}'".format)

輸出

>>> print(test_dataframe[['id2', 'sep']].to_string(index=False))

  id sep
 'A'   ,
 'B'   ,
 'C'   ,
 'D'   ,

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章