Pandas 读取制表符分隔的 csv

问题描述 投票:0回答:2

我在为我的学士论文绘制负载曲线时遇到问题。

我有一个 csv 文件,用制表符分隔,如下所示:

PV-Energie (AC) abzgl. Standby-Verbrauch, -0,01747 -0,01747 -0,01747 -0,01747 -0,01747 -0,01747 -0,01747 -0,05512 0,1418 72,219 87,723 89,751 41,49 33,484 27,917 3,5819 -0,05512 -0,01747 -0,01747 -0,01747 -0,01747 -0,01747 -0,01747 -0,01747 -0,01747

是否可以选择“tab”作为分隔符?

这是我的代码:

from oemof import solph
import pandas as pd
import matplotlib.pyplot as plt
from oemof.tools import economics
import numpy as np
from oemof.tools import logger

logger.define_logging()

logger.info('Programm gestartet')

filename = 'PV.csv'
df = pd.read_csv(filename)

logger.info('Datei wurde eingelesen')

ax = df.plot.area(subplots=True, figsize=(10,7))

ax[0].set_ylabel("Leistung in kW")
ax[0].set_xlabel("Zeit in h")

plt.title('power demand')
plt.tight_layout()
plt.show()
logger.info('Programm beendet')

我想找到解决我的问题的方法。

python pandas dataframe charts
2个回答
0
投票

复制并粘贴您提供的示例似乎没有用制表符分隔。我建议使用像Notepad++这样的文本编辑器来用制表符(

\t
)替换空格。通过这种方式,我得到了以下内容:

PV-Energie  (AC) abzgl. Standby-Verbrauch,
-0,01747     -0,01747    -0,01747
-0,01747     -0,01747    -0,01747
-0,01747     -0,05512    0,1418
72,219  87,723  89,751
41,49   33,484  27,917
3,5819  -0,05512     -0,01747
-0,01747     -0,01747    -0,01747
-0,01747     -0,01747    -0,01747

然后我用制表符作为分隔符读取了 csv:

df = pd.read_csv(
    r"C:\Users\tab_separated.csv",
    sep=r'\t'
)

获得以下内容:

|    | PV-Energie   | (AC) abzgl.   | Standby-Verbrauch,   |
|---:|:-------------|:--------------|:---------------------|
|  0 | -0,01747     | -0,01747      | -0,01747             |
|  1 | -0,01747     | -0,01747      | -0,01747             |
|  2 | -0,01747     | -0,05512      | 0,1418               |
|  3 | 72,219       | 87,723        | 89,751               |
|  4 | 41,49        | 33,484        | 27,917               |
|  5 | 3,5819       | -0,05512      | -0,01747             |
|  6 | -0,01747     | -0,01747      | -0,01747             |
|  7 | -0,01747     | -0,01747      | -0,01747             |

0
投票

检查此代码片段,

import pandas as pd

#read the csv file into a pandas dataframe, sep flag set to \t
df = pd.read_csv('filename.csv', sep='\t')

print(df)
© www.soinside.com 2019 - 2024. All rights reserved.