如何在Panda中以CSV格式定义标题。然后在分析数据后将其推入新文件。如何将标题包含在新的CSV文件中
输入:
John Apple <-- Header Start
9/21/2005
Duration: 00:00:06 <-- Header End
Time Body_Temp Thermistor <-- Index
00:00:00 0.00
00:00:01 88.07
00:00:02 88.07
00:00:03 83.90
00:00:04 104.35
00:00:05 85.43
00:00:06 85.43
输出文件:
John Apple <-- Header Start
9/21/2005
Duration: 00:00:06 <-- Header End
Time Body_Temp Thermistor <-- Index
00:00:00 0.00
00:00:01 88.07
00:00:02 88.07
00:00:03 83.90
00:00:04 104.35 <--Points this is above 100
00:00:05 85.43
00:00:06 85.43
我的代码到目前为止:
from pandas import DataFrame, read_csv
import csv
import pandas as pd
import numpy as np
file = r'Alpha.csv'
df = pd.read_csv(file)
uncommon = df.loc[(df['Temp'] >= 100)]
#can only figure out to find any temp above 100 and print it into a new CSV
uncommon.to_csv('Dummy.csv',sep='\t')
答案 0 :(得分:1)
如果我理解正确,这样的事情应该有效:
file = r'Alpha.csv'
meta = pd.read_csv(file, nrows=4, header=None)
df = pd.read_csv(file, skiprows=4)
uncommon = df.loc[(df['Temp'] >= 100)]
with open('Dummy.csv', 'w') as out:
meta.to_csv(out, index=False, header=False)
uncommon.to_csv(out, sep='\t', index=False)
<强>解释强>
meta
,将实际数据读入data
数据框。data
上执行操作并分配到uncommon
。meta
功能将uncommon
和open
分别写入csv文件。替代工作流程
但我的偏好是保持数据清洁。例如,为什么不单独存储一个表与链接到文件名的元数据?
您当前的方法将阻止您使用元数据进行任何有意义的分析。