python - 熊猫数据框,如果条件满足则跳过索引
问题描述
我只想在满足特定条件时才跳过数据帧输出的索引。A
这是卖家的特殊ID。B
这是售价。类型是销售类型。
import pandas
Data= []
Data+=[{"A": "ID_1", "Special":{"type": "USD"},"B": "0.11",}]
Data+=[{"A": "ID_2", "Special":{"type": "EUR"},"B": "0.122",}]
Data+=[{"A": "ID_3", "Special":{"type": "EUR"},"B": "0.1444",}]
Data = pandas.DataFrame(Data)
Data = Data.sort_values(by=['B'], ascending=False)
seller = Data['A'][Data.index[-1]]
price = Data['B'][Data.index[-1]]
print('{} {}'.format(seller, price))
如您所见,我将有很多记录,pandas 将迭代数据并为每个记录分配一个索引,{ ... }
还将根据 B 值(即价格)进行排序,它将显示最低价格。
我想要一张支票来显示最低价格和卖家,只要类型是欧元(忽略所有类型为美元的记录)......我该怎么做?我试图尽可能地解释......谢谢。
解决方案
我认为你可以这样做:
# gets only the Euro type
euro_rows = Data.loc[Data["Special"] == {'type': 'EUR'}]
# gets the row of the lowest price
lowest_price = euro_rows.loc[euro_rows['B'] == euro_rows['B'].min()]
# gets seller and price
seller, price = lowest_price['A'].item(), lowest_price['B'].item()
print(seller) #prints: IDDDDDDDDDDD
print(price) #prints: 0.1
推荐阅读
- postgresql - 如何解决空 docker-entrypoint-initdb.d 的问题?(PostgreSQL + Docker)
- python - Python:通过将对象分配给另一个对象来修改它
- javascript - 让 ng-view 只显示它的内容,而不是它本身
- docker - 如何在 Docker 上设置 Apache Spark 和 Zeppelin
- c++ - C++17/20 并行算法:对对象的非同步访问
- postgresql - postgres - 将列值插入为文字'null'
- javascript - 滚动时,Material VueJS组件未固定在底部页面上
- android - 我正在尝试使用“Pdf 创建库”在颤振应用程序中生成 pdf。在用其他语言生成 pdf 时会出现异常
- c# - Convert OpenImage Coordinates to DarkNet format
- java - 如何在 GridPane 中对齐 ListView 基线