ZacAttack
ZacAttack

Reputation: 1055

df.iloc[1].ColumnName Is returning the entire row, not the one specific value?

Overview
I am trying to get the value of my tickers column(T) and use that for a file name.

Approach
I am using the df.iloc[1].T and the column name to capture one specific value and then I want to take that value and concatenate with another string to create a file path.

Problem
df.iloc[1].T is giving me the whole entire row not just the one value.

print(df2.iloc[1].T)            
Unnamed: 0              1
D              2010-12-01
T                    tvix
O                   99.98
H                  100.69
L                    98.8
C                  100.69
V                       0
AC            2.51725e+06
Y                    2010
dow                     3
M                      12
W                      48
Name: 1, dtype: object

I am expecting to get "tvix". Now when I print any other column I get the one value for example

print(df2.iloc[1].D)
print(df2.iloc[1].H)
print(df2.iloc[1].L)

2010-12-01
100.689997
98.799998                                   

It prints the Date,High,and Low as expected. Now the difference between the tickers column(T) and all the others is that the ticker column has the same value for every row(I have it this way for groupBy purposes)

print(df2.head())
   Unnamed: 0           D     T           O           H           L  \
0           0  2010-11-30  tvix  106.269997  112.349997  104.389997
1           1  2010-12-01  tvix   99.979997  100.689997   98.799998
2           2  2010-12-02  tvix   98.309998   98.309998   86.499998
3           3  2010-12-03  tvix   88.359998   88.359998   78.119998
4           4  2010-12-06  tvix   79.769998   79.999998   74.619998

I am assuming that since the T column has the same value all the way down, that this is reason I am having this problem. Any input on this would be greatly appreciated.

Upvotes: 1

Views: 2219

Answers (2)

Mohammad Yusuf
Mohammad Yusuf

Reputation: 17074

You can specify the column in iloc[]

df.iloc[1,2]

This will get you 'tvix'

Or you can use mix indexing with .ix[]

df.ix[1,'T']

Upvotes: 3

user2285236
user2285236

Reputation:

Accessing columns via .column_name is a little problematic (same for indices). It doesn't always work (when the column name has spaces, when it is a number or like in this case, when a method or an attribute has the same name). .T is for transposing a DataFrame so you should use the brackets:

df.iloc[1]['T']

Upvotes: 4

Related Questions