[英]How can I extract columns of data from a CSV file and define them as x and y variables, then plot them in python using pylab?
[英]Import data from .txt file and define x and y variables in Python
我是Python的新手,我需要導入以空格分隔的數據。
數據看起來像這樣:
Title one
Title two
Title three
Title four
1.2944870E-03 7.1226442E-01
3.8834610E-03 8.3775342E-01
6.4724353E-03 1.0313828E+00
9.0614092E-03 7.7915078E-01
2.2006279E-02 1.1677371E+00
我需要在導入過程中跳過前4行。
到目前為止,我有這個:
# Get .txt file
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(filetypes=[("Two Column txt","*.txt"),("Any text file","*.*")])
# Read the data
import pandas as pd
data = pd.read_csv(file_path,skiprows=4)
用pd.read_csv導入后,數據以[ x _行數,1列]的形式出現。 我想知道是否有人可以告訴我如何將其分開並將第一列分配給X ,將第二列分配給Y
最好的祝福!
由於文件是用空格分隔的,因此您應該使用sep=' '
覆蓋默認的逗號分隔解析。 為了提供自己的標題,可以使用header
和names
參數:
data = pd.read_csv(file_path, sep=' ', skiprows=8, header=None, names=['X','Y'])
我可以用導入文件
data = pd.read_csv(file_path, sep='\\s+', skiprows=4, header=None, names=['X','Y'])
然后我將變量分隔為:
X=data['X'] Y=data['Y']
謝謝大家的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.