[英]Load CSV to Pandas MultiIndex DataFrame
I have a 719mb CSV file that looks like: 我有一个719mb的CSV文件,看起来像:
from, to, dep, freq, arr, code, mode (header row)
RGBOXFD,RGBPADTON,127,0,27,99999,2
RGBOXFD,RGBPADTON,127,0,33,99999,2
RGBOXFD,RGBRDLEY,127,0,1425,99999,2
RGBOXFD,RGBCHOLSEY,127,0,52,99999,2
RGBOXFD,RGBMDNHEAD,127,0,91,99999,2
RGBDIDCOTP,RGBPADTON,127,0,46,99999,2
RGBDIDCOTP,RGBPADTON,127,0,3,99999,2
RGBDIDCOTP,RGBCHOLSEY,127,0,61,99999,2
RGBDIDCOTP,RGBRDLEY,127,0,1430,99999,2
RGBDIDCOTP,RGBPADTON,127,0,115,99999,2
and so on...
I want to load in to a pandas DataFrame. 我想加载到熊猫DataFrame中。 Now I know there is a load from csv method:
现在我知道csv方法有负载:
r = pd.DataFrame.from_csv('test_data2.csv')
But I specifically want to load it as a 'MultiIndex' DataFrame where from and to are the indexes: 但我特别想将其加载为“ MultiIndex” DataFrame,其中from和to是索引:
So ending up with: 所以最后以:
dep, freq, arr, code, mode
RGBOXFD RGBPADTON 127 0 27 99999 2
RGBRDLEY 127 0 33 99999 2
RGBCHOLSEY 127 0 1425 99999 2
RGBMDNHEAD 127 0 1525 99999 2
etc. I'm not sure how to do that? 等等。我不确定该怎么做?
You could use pd.read_csv
: 您可以使用
pd.read_csv
:
>>> df = pd.read_csv("test_data2.csv", index_col=[0,1], skipinitialspace=True)
>>> df
dep freq arr code mode
from to
RGBOXFD RGBPADTON 127 0 27 99999 2
RGBPADTON 127 0 33 99999 2
RGBRDLEY 127 0 1425 99999 2
RGBCHOLSEY 127 0 52 99999 2
RGBMDNHEAD 127 0 91 99999 2
RGBDIDCOTP RGBPADTON 127 0 46 99999 2
RGBPADTON 127 0 3 99999 2
RGBCHOLSEY 127 0 61 99999 2
RGBRDLEY 127 0 1430 99999 2
RGBPADTON 127 0 115 99999 2
where I've used skipinitialspace=True
to get rid of those annoying spaces in the header row. 在这里我使用
skipinitialspace=True
摆脱了标题行中那些烦人的空格。
from_csv() works similarly: from_csv()的工作方式类似:
import pandas as pd
df = pd.DataFrame.from_csv(
'data.txt',
index_col = [0, 1]
)
print df
--output:--
dep freq arr code mode
from to
RGBOXFD RGBPADTON 127 0 27 99999 2
RGBPADTON 127 0 33 99999 2
RGBRDLEY 127 0 1425 99999 2
RGBCHOLSEY 127 0 52 99999 2
RGBMDNHEAD 127 0 91 99999 2
RGBDIDCOTP RGBPADTON 127 0 46 99999 2
RGBPADTON 127 0 3 99999 2
RGBCHOLSEY 127 0 61 99999 2
RGBRDLEY 127 0 1430 99999 2
RGBPADTON 127 0 115 99999 2
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.from_csv.html#pandas.DataFrame.from_csv http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.from_csv.html#pandas.DataFrame.from_csv
From this discussion, 通过讨论,
https://github.com/pydata/pandas/issues/4916 https://github.com/pydata/pandas/issues/4916
it looks like read_csv() was implemented to allow you to set more options, which makes from_csv() superfluous. 看起来好像实现了read_csv()来允许您设置更多选项,这使from_csv()成为多余。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.