使用熊猫将逗号添加到CSV文件

时间:2020-03-19 19:17:55

标签: python pandas matplotlib visualization

我有一个要从Linux sosreport 获取的文件,我想制作一个饼图,问题是将文件转换为CSV时,它不包含逗号并创建它作为1列。

文件如下:

MemFree:      11988456 kB
Buffers:         47456 kB
Cached:        2304036 kB
SwapCached:        772 kB
Active:        3625348 kB
Inactive:       397832 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:     16306056 kB
LowFree:      11988456 kB
SwapTotal:     8191992 kB
SwapFree:      8122928 kB
Dirty:           50552 kB
Writeback:           0 kB
AnonPages:     1434016 kB
Mapped:        2065024 kB
Slab:          123484 kB
PageTables:     121432 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
CommitLimit:  16345020 kB
Committed_AS: 5883888 kB
VmallocTotal: 34359738367 kB
VmallocUsed:    267500 kB
VmallocChunk: 34359470295 kB
HugePages_Total:     0
HugePages_Free:      0
HugePages_Rsvd:      0
Hugepagesize:     2048 kB

将meminfo文件转换为CSV文件,如下所示:

df.to_csv('meminfo.csv', header=False, index=False)

并且我也在尝试命名列,但我不能。

对不起,但我是数据可视化领域的新手

1 个答案:

答案 0 :(得分:2)

通过使用python引擎,您可以将分隔符定义为正则表达式。添加更多字段来处理标题,索引和数据类型,这应该可以为您提供所需的信息

>>> df=pd.read_csv("test.txt", engine="python", sep=r":?\s+", index_col=0,
... names=["field", "value", "unit"],
... dtype={"field":str, "value":int, "unit":str})
>>> 
>>> df
                       value  unit
field                             
MemFree             11988456    kB
Buffers                47456    kB
Cached               2304036    kB
SwapCached               772    kB
Active               3625348    kB
Inactive              397832    kB
HighTotal                  0    kB
HighFree                   0    kB
LowTotal            16306056    kB
LowFree             11988456    kB
SwapTotal            8191992    kB
SwapFree             8122928    kB
Dirty                  50552    kB
Writeback                  0    kB
AnonPages            1434016    kB
Mapped               2065024    kB
Slab                  123484    kB
PageTables            121432    kB
NFS_Unstable               0    kB
Bounce                     0    kB
CommitLimit         16345020    kB
Committed_AS         5883888    kB
VmallocTotal     34359738367    kB
VmallocUsed           267500    kB
VmallocChunk     34359470295    kB
HugePages_Total            0  None
HugePages_Free             0  None
HugePages_Rsvd             0  None
Hugepagesize            2048    kB
>>>