Issue
Hopefully a fairly simple answer to my issue.
When I run the following code:
print (data_1.iloc[1])
I get a nice, vertical presentation of the data, with each column value header, and its value presented on separate rows. This is very useful when looking at 2 sets of data, and trying to find discrepancies.
However, when I write the code as:
print (data_1.loc[data_1["Name"].isin(["John"])])
I get all the information arrayed across the screen, with the column header in 1 row, and the values in another row.
My question is: Is there any way of using the second code, and getting the same vertical presentation of the data?
Solution
The difference is that data_1.iloc[1]
returns a pandas Series
whereas data_1.loc[data_1["Name"].isin(["John"])]
returns a DataFrame
. Pandas has different representations for these two data types (i.e. they print differently).
The reason iloc[1]
gives you a Series
is because you indexed it using a scalar. If you do data_1.iloc[[1]]
you'll see you get a DataFrame
instead. Conversely, I'm assuming that data_1["Name"].isin(["John"])
is returning a collection. If you wanted to get a Series
instead you might try something like
print(data_1.loc[data_1["Name"].isin(["John"])[0]])
but only if you're sure you're getting one element back.
Answered By - Dan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.