I have some data stored in a file (xyz.dat) in the following format:
我有一些数据存储在文件(xyz.dat)中,格式如下:
1 0.997790664988225E+000 0.100177762164687E+001 0.100034262755073E+001
2 0.997788473973293E+000 0.100177969047903E+001 0.100034273413754E+001
3 0.997782981718032E+000 0.100178486093194E+001 0.100034301665219E+001
What I want to do is, for each row, store the first column as x
and rest of the values as y[1]
, y[2]
, y[3]
...so on. Later I want to plot the data stored in y[n]
for each row separately. I am new to python and have tried using split() but did not have much success. I am either not able to separate out the first column or at the end of each row there is a trailing '\n'
. Any help will be really appreciated. Thanks in advance.
我想要做的是,对于每一行,将第一列存储为x,将其余值存储为y [1],y [2],y [3] ......等等。后来我想分别为每一行绘制存储在y [n]中的数据。我是python的新手,并尝试过使用split()但没有取得多大成功。我要么无法将第一列分开,要么在每一行的末尾都有一个尾随'\ n'。任何帮助将非常感激。提前致谢。
2 个解决方案
#1
0
You could read and plot as follows:
你可以阅读和绘图如下:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('xyz.dat', sep=' ', header=None, usecols=(1, 2, 3)).T
df.plot(lw=0.5)
plt.show()
This would give you output:
这会给你输出:
Your 3 rows contain very similar data.
您的3行包含非常相似的数据。
If the number of columns is unknown, you could just read it in normally and drop the first CSV column (actually first row after being transposed):
如果列数未知,您可以正常读取它并删除第一个CSV列(实际上是转置后的第一行):
df = pd.read_csv('xyz.dat', sep=' ', header=None).T.drop(0)
df.plot(lw=0.5)
plt.show()
#2
0
You can try:
你可以试试:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_fwf('file.dat')
plt.plot(df.iloc[:, 0], df.iloc[:,1])
plt.show()
You can go about plotting in the manner shown above. Hope this helps.
您可以按照上面显示的方式进行绘图。希望这可以帮助。
#1
0
You could read and plot as follows:
你可以阅读和绘图如下:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('xyz.dat', sep=' ', header=None, usecols=(1, 2, 3)).T
df.plot(lw=0.5)
plt.show()
This would give you output:
这会给你输出:
Your 3 rows contain very similar data.
您的3行包含非常相似的数据。
If the number of columns is unknown, you could just read it in normally and drop the first CSV column (actually first row after being transposed):
如果列数未知,您可以正常读取它并删除第一个CSV列(实际上是转置后的第一行):
df = pd.read_csv('xyz.dat', sep=' ', header=None).T.drop(0)
df.plot(lw=0.5)
plt.show()
#2
0
You can try:
你可以试试:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_fwf('file.dat')
plt.plot(df.iloc[:, 0], df.iloc[:,1])
plt.show()
You can go about plotting in the manner shown above. Hope this helps.
您可以按照上面显示的方式进行绘图。希望这可以帮助。