![](/img/trans.png)
[英]How can I get the name of the column if the value ends with specific string from pandas dataframe
[英]How can I get a specific value from a pandas DataFrame?
我有一個看起來像這樣的.df
( df = pandas.read_csv(main_db)
):
項目名 | 項目品牌 | 項目代碼 | 物品庫存 |
---|---|---|---|
一些名字 | 一些品牌 | 一個 6 位數字 | 一些低數字 |
更多的名字 | 更多品牌 | 諾爾 6 位數字 | 更多庫存 |
看起來像,但有實際的名稱和品牌。
現在,如果我使用result = df[df['itemCode']==itemCode]
,我會得到:
空白的 | 項目名 | 項目品牌 | 項目代碼 | 物品庫存 |
---|---|---|---|---|
6 | 項目名稱7 | itembrand7 | 905616 | 13 |
這很好。 我花了太長時間尋找這個。 現在,我只想讓itemStock
(在本例中為 13)在其他地方使用。 所以在這里我使用result2 = result['itemStock']
。
6 | 13 |
---|
Name: itemStock, dtype: int64
嗯。 好吧,不是我想要的。 我該怎么做才能只得到13
?
如果您使用:
result2 = result['itemStock']
它會為您返回一個數據系列,因此您還可以看到索引,而不僅僅是您想要的值。 您可以使用
type(result2)
你可以使用 .values 屬性找到你想要的
result2 = result['itemStock'].values
嘗試
result = df.loc[(['itemCode']==itemCode, 'itemStock')].iat[0]
或者只是 iat[0] 在你的第二個結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.