with open('f.csv') as file:
#read first 2 rows to metadata
header = [file.readline() for x in range(2)]
meta = [value.strip().split(',') for value in header]
print (meta)
[['sometext', ' sometext2'], ['moretext', ' moretext1', ' moretext2']]
df = pd.read_csv(file)
print (df)
*header*
0 actual data
Note, however, that while you can attach attributes to a DataFrame,
operations performed on the DataFrame (such as groupby, pivot, join or
loc to name just a few) may return a new DataFrame without the
metadata attached. Pandas does not yet have a robust method of
propagating metadata attached to DataFrames.
您可以先读取元数据,然后使用
read_csv
:Pandas设计用于读取结构化数据
对于非结构化数据,只需使用内置的^{} :
您可以像这样将字符串附加到数据帧:
But note:
相关问题 更多 >
编程相关推荐