Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

python - How to get column by number in Pandas?

What's the difference between:

Maand['P_Sanyo_Gesloten']
Out[119]: 
Time
2012-08-01 00:00:11    0
2012-08-01 00:05:10    0
2012-08-01 00:10:11    0
2012-08-01 00:20:10    0
2012-08-01 00:25:10    0
2012-08-01 00:30:09    0
2012-08-01 00:40:10    0
2012-08-01 00:50:09    0
2012-08-01 01:05:10    0
2012-08-01 01:10:10    0
2012-08-01 01:15:10    0
2012-08-01 01:25:10    0
2012-08-01 01:30:10    0
2012-08-01 01:35:09    0
2012-08-01 01:40:10    0
...
2012-08-30 22:35:09    0
2012-08-30 22:45:10    0
2012-08-30 22:50:09    0
2012-08-30 22:55:10    0
2012-08-30 23:00:09    0
2012-08-30 23:05:10    0
2012-08-30 23:10:09    0
2012-08-30 23:15:10    0
2012-08-30 23:20:09    0
2012-08-30 23:25:10    0
2012-08-30 23:35:09    0
2012-08-30 23:40:10    0
2012-08-30 23:45:09    0
2012-08-30 23:50:10    0
2012-08-30 23:55:11    0
Name: P_Sanyo_Gesloten, Length: 7413, dtype: int64

And

Maand[[1]]
Out[120]: 
&ltclass 'pandas.core.frame.DataFrame'&gt
DatetimeIndex: 7413 entries, 2012-08-01 00:00:11 to 2012-08-30 23:55:11
Data columns (total 1 columns):
P_Sanyo_Gesloten    7413  non-null values
dtypes: int64(1)

How can I get data by column-indexnumber? And not by an Index-string?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One is a column (aka Series), while the other is a DataFrame:

In [1]: df = pd.DataFrame([[1,2], [3,4]], columns=['a', 'b'])

In [2]: df
Out[2]:
   a  b
0  1  2
1  3  4

The column 'b' (aka Series):

In [3]: df['b']
Out[3]:
0    2
1    4
Name: b, dtype: int64

The subdataframe with columns (position) in [1]:

In [4]: df[[1]]
Out[4]:
   b
0  2
1  4

Note: it's preferable (and less ambiguous) to specify whether you're talking about the column name e.g. ['b'] or the integer location, since sometimes you can have columns named as integers:

In [5]: df.iloc[:, [1]]
Out[5]:
   b
0  2
1  4

In [6]: df.loc[:, ['b']]
Out[6]:
   b
0  2
1  4

In [7]: df.loc[:, 'b']
Out[7]:
0    2
1    4
Name: b, dtype: int64

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...