У меня есть следующий текстовый файл в качестве входных данных (infile.txt
)
A foo 3.0
A bar 3.1
B foo 3.0
B bar 3.1
И со следующим кодом
import pandas as pd
infile="infile.txt"
df = pd.io.parsers.read_table(infile,header=None,sep=" ")
df.columns = ['sample','celltype','score']
dfp = df.pivot(index='sample',columns='celltype',values='score')
dfp_prc = dfp.applymap(lambda x: x * 100)
dfp_prc.to_csv("out.txt",sep=" ", index=True)
он создает этот CSV:
sample bar foo
A 310.0 300.0
B 310.0 300.0
То, что я хочу сделать тогда, дается список:
mlist = ['sample','XXX','YYY']
Я хотел бы получить окончательный файл CSV, который выглядит так:
sample XXX YYY
sample bar foo
A 310.0 300.0
B 310.0 300.0
Как это удобно сделать в Pandas?
lineterminator
кcsv.writer
- см. мой отредактированный ответ и документацию здесь: docs.python.org/2/library/ 30.01.2015