简体   繁体   English

如何在不知道 pandas 中的索引的情况下打印行 x

[英]How to print row x without knowing the index in pandas

I have a Dataframe with 45 columns and 11k rows.我有一个 45 列和 11k 行的 Dataframe。 This dataframe consist of players.这个 dataframe 由播放器组成。 Columns displaying their name, player_id, rating, height etc. Pretend you have the name of a player in the dataframe, or their ID, and you want to access the entire row of that player.显示他们的姓名、player_id、等级、身高等的列。假设您有 dataframe 中的玩家的姓名或他们的 ID,并且您想访问该玩家的整行。 You want to see all the information of that individual, but you only have one unique identifyer.您想查看该个人的所有信息,但您只有一个唯一标识符。

I tried using df.loc[[id_number]] , but that only takes me to the index of the dataframe, which does not correspond to player_id.我尝试使用df.loc[[id_number]] ,但这只会将我带到 dataframe 的索引,它与 player_id 不对应。

Hopefully I explained it well enough.希望我解释得足够好。 If you have any questions, please post them below.如果您有任何问题,请在下面发布。

df.loc[df['column_name'] == some_value]

Related to https://stackoverflow.com/a/17071908/17487637https://stackoverflow.com/a/17071908/17487637相关

You can try applying a mask:您可以尝试使用面膜:

df[df.playerId == id_number]

Assuming playerId is the name of the column containing the player ids.假设playerId是包含玩家 ID 的列的名称。

As far as I have understood, you want to query a dataframe based on one unique identifier.据我了解,您想根据一个唯一标识符查询 dataframe 。 Let's suppose you only have player name .假设您只有player name

df[df.player_name==playername]

Here playername is the variable where you will store your desired player name.这里 playername 是您将存储所需玩家名称的变量。

I use this in my code to find what i need in excel file.我在我的代码中使用它来在 excel 文件中找到我需要的内容。 Mayby will be helpful for you. Mayby 将对您有所帮助。

 search = input('\t\t    Find row:  ')
 xlsxfile = pd.read_excel('your_filename.xlsx', engine='openpyxl')
df = xlsxfile[xlsxfile['name_of_your_column' ].str.contains(search , na=False)]

dx = (df[['name', 'player_id','height']])
print(dx) 

You will need some third party module for this like openpyxl.为此,您将需要一些第三方模块,例如 openpyxl。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM