python - 使用python读取文本文件数据

标签 python list dataframe

我有一个文本文件,其中包含许多列和行,并且具有多种数据类型。我想用 python 读取文件并通过选择列来绘制值。 我的文件如下所示:

    time        column1        column2        column3        column4        column5        column6        column7 
 100.035   6.667252E+00  -4.106210E+00  -1.577542E-02   4.090584E+00  -3.699584E-01  -6.998253E-02  -6.699544E-01 
 100.075   6.776713E+00  -4.347899E+00  -1.791951E-02   4.329726E+00  -3.699584E-01  -6.998253E-02  -6.699544E-01 
 100.115   6.806808E+00  -4.451121E+00  -1.886022E-02   4.432934E+00  -3.699584E-01  -6.998253E-02  -6.699544E-01 
 100.155   6.826516E+00  -4.534202E+00  -1.924360E-02   4.513488E+00  -3.699584E-01  -6.998253E-02  -6.699544E-01 
 100.195   6.890967E+00  -4.962194E+00  -1.946191E-02   4.943943E+00  -3.699584E-01  -6.998253E-02  -6.699544E-01 
 100.235   6.961544E+00  -5.430468E+00  -1.924892E-02   5.409640E+00  -3.699584E-01  -6.998253E-02  -6.699544E-01 

我尝试读取上述文件 herehere并尝试了一些基于模式的分隔代码如 here 。到目前为止,下面代码的输出将所有列都限制在 first_columns 处,如列出的

import csv
with open ('mps50.txt', 'r') as f:
     first_column = [row[0] for row in csv.reader(f,delimiter='\t')]

但是 first_column 是一个列表,我想不出如何进一步使用它来帮助我绘制值。你能指导我如何做到这一点吗?一些示例或链接会有帮助。 first_column looks like this.

最佳答案

使用pandas:

  • 使用pandas.read_csv读取数据
    • 这假设数据如图所示,位于 txt 文件中,并以空格作为分隔符。
  • 使用matplotlib进行绘图
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('test.txt', sep='\\s+')

# df view
    time   column1   column2   column3   column4   column5   column6   column7
 100.035  6.667252 -4.106210 -0.015775  4.090584 -0.369958 -0.069983 -0.669954
 100.075  6.776713 -4.347899 -0.017920  4.329726 -0.369958 -0.069983 -0.669954
 100.115  6.806808 -4.451121 -0.018860  4.432934 -0.369958 -0.069983 -0.669954
 100.155  6.826516 -4.534202 -0.019244  4.513488 -0.369958 -0.069983 -0.669954
 100.195  6.890967 -4.962194 -0.019462  4.943943 -0.369958 -0.069983 -0.669954
 100.235  6.961544 -5.430468 -0.019249  5.409640 -0.369958 -0.069983 -0.669954

绘制数据:

  • 有许多用于绘制数据的选项。
    • 以下是一些简单的示例
# all columns
plt.plot(df['time'], df.iloc[:, 1:], marker='o')
plt.xticks(rotation=90)
plt.show()

enter image description here

# specific column
plt.plot(df['time'], df['column1'], marker='o')
plt.xticks(rotation=90)
plt.show()

enter image description here

  • seaborn
import seaborn as sns

# set the index
df_ind = df.set_index('time')

sns.lineplot(data=df_ind, dashes=False, markers=True)
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.xticks(rotation=90)
plt.show()

enter image description here

关于python - 使用python读取文本文件数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58783180/

相关文章:

python / Pandas : create summary table

python - 简单的 Python TCP 服务器不适用于 Amazon EC2 实例

python - 我应该使用哪个 Enthought EPD 发行版?

python - 有没有支持添加/删除用户的python xmpp库?

python - 如何将占位符文本添加到 Django Admin 中的搜索框?

python - 比较两个系列时,如何根据一个系列进行过滤?

list - Scala:将 map 大小 n 拆分为列表( map 最大大小为 3)

java - ArrayList<Track> 的排序

python - 将列表平均分成两半的最有效方法是什么

python - 使用 pandas dataframe.query() 选择列