我真的不明白我在做什么。我有两个数据框。一个包含列标签列表,另一个包含一堆数据。我只想用列标签标记数据中的列。
我的代码:
airportLabels = pd.read_csv('airportsLabels.csv', header= None)
airportData = pd.read_table('airports.dat', sep=",", header = None)
df = DataFrame(airportData, columns = airportLabels)
当我这样做时,所有数据都变成“ NaN”,并且现在只有一列了。我真的很困惑。
我想你需要添加参数nrows
来read_csv
,如果需要只读列,删除header= None
,因为第一行csv
是列名,然后使用参数names
在read_table
同columns
来自DataFrame
airportLabels
:
import pandas as pd
import io
temp=u"""col1,col2,col3
1,5,4
7,8,5"""
#after testing replace io.StringIO(temp) to filename
airportLabels = pd.read_csv(io.StringIO(temp), nrows=0)
print airportLabels
Empty DataFrame
Columns: [col1, col2, col3]
Index: []
temp=u"""
a,d,f
e,r,t"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_table(io.StringIO(temp), sep=",", header = None, names=airportLabels.columns)
print df
col1 col2 col3
0 a d f
1 e r t
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句