简体   繁体   中英

Python: Load CSV, first column as row names, first row as column names

I want to load a CSV file using Python2.7, in which the first row contains column names and the first column contains row names.

My CSV file looks like beneath.

  A  B  C  D
a 1. 2. 3. 4. 
b 5. 6. 7. 8. 

I don't know how to do that with numpy or pandas. Can someone enlighten me ? Thanks !

You can use read_csv with separator s\\+ - arbitrary whitespace:

import pandas as pd
import io

temp=u"""A B C D
a 1. 2. 3. 4.
b 5. 6. 7. 8."""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), sep="\s+")
print df
     A    B    C    D
a  1.0  2.0  3.0  4.0
b  5.0  6.0  7.0  8.0

Docs

You could use pd.read_csv with regex separator \\s+ :

import pandas as pd

In [4]: pd.read_csv('file.csv', sep='\s+')
Out[4]:
     A    B    C    D
a  1.0  2.0  3.0  4.0
b  5.0  6.0  7.0  8.0

Or you could use delim_whitespace argument for that:

In [5]: pd.read_csv('file.csv', delim_whitespace=True)
Out[5]:
     A    B    C    D
a  1.0  2.0  3.0  4.0
b  5.0  6.0  7.0  8.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM