Aug-17-2024, 08:20 AM
Suppose we have the following DataFrame recording the results of 5 students.
I then try to get Tom's Java result. I thought I should give the row index first and write
dic0 = {'Java':[87,65,26,89,67],
'C++':[63,98,66,89,80],
'Python':[78,25,76,43,69]}
d = pd.DataFrame(dic0)
d.index = ['Tom', 'Bob', 'Tim', 'Wien', 'Lily']d[1:] should give the rows from index 1 to the end. The number inside square brackets is referring to the rows.Output: Java C++ Python
Bob 65 98 25
Tim 26 66 76
Wien 89 89 43
Lily 67 80 69But if I write d['Java'], the system will consider the thing inside sqaure brackets as column index. The output is:Output:Tom 87
Bob 65
Tim 26
Wien 89
Lily 67
Name: Java, dtype: int64I am confused here. In both examples, I just insert one 'item' in the square brackets, but the system regards the first one as row index while the second one column index. What is the rule behind this? How can I know in what ways the system will interpret my input?I then try to get Tom's Java result. I thought I should give the row index first and write
d['Tom']['Java'] but it turns out that I should write d['Java']['Tom']. In other words, I should extract the 'Java' columns, then look for 'Tom'.Output:87My next job is to get Tim's result of all three subjects, and I try to use d.loc[]. This time, I need to give the the row index first and the colum index. Hence, I should write d.loc['Tim',:]. Output:Java 26
C++ 66
Python 76
Name: Tim, dtype: int64But now I am confused. When we try to extract elements from DataFrame, sometimes we give the row index first while sometimes it's the opposite. Is that a general rule behind this? Or do I just have to memorize the different requirements of the ways of extracting elements?
