Each Answer to this Q is separated by one/two green lines.
Say I have the following DataFrame
Letter Number A 1 B 2 C 3 D 4
Which can be obtained through the following code
import pandas as pd letters=pd.Series(('A', 'B', 'C', 'D')) numbers=pd.Series((1, 2, 3, 4)) keys=('Letters', 'Numbers') df=pd.concat((letters, numbers), axis=1, keys=keys)
Now I want to get the value C from the column Letters.
The command line
2 C Name: Letters, dtype: object
How can I get only the value C and not the whole two line output?
This returns the first element in the Index/Series returned from that selection. In this case, the value is always the first element.
Or you can run a loc() and access the first element that way. This was shorter and is the way I have implemented it in the past.
values attribute to return the values as a np array and then use
 to get the first value:
In : df.loc[df.Letters=='C','Letters'].values Out: 'C'
I personally prefer to access the columns using subscript operators:
df.loc[df['Letters'] == 'C', 'Letters'].values
This avoids issues where the column names can have spaces or dashes
- which mean that accessing using
import pandas as pd dataset = pd.read_csv("data.csv") values = list(x for x in dataset["column name"]) >>> values 'item_0'
actually, you can just index the dataset like any old array.
import pandas as pd dataset = pd.read_csv("data.csv") first_value = dataset["column name"] >>> print(first_value) 'item_0'
You can use
loc with the index and column labels.
df.loc[2, 'Letters'] # 'C'
If you prefer the “Numbers” column as reference, you can set it as index.
I find this cleaner as it does not need the
I think a good option is to turn your single line DataFrame into a Series first, then index that: