【发布时间】:2020-12-18 04:46:15
【问题描述】:
我想绘制一个密度图,需要从数据中提取两个值用作坐标,第三个值将作为密度值。我有一个要读取的文本文件,当一个行/列值与某个条件匹配时,我想保存该行的另外两个值。我有一些适用于其中一种条件的东西,但它将行值保存在嵌套列表中,我想知道是否有更 Pythonic 的方式来执行此操作,因为我认为以后可能更容易绘制。
数据:
Accum EdgeThr NumberOfBlobs durationMin Vol Perom X Y
50 0 0 0.03 0 0 0 0
50 2 0 0.03 0 0 0 0
50 4 0 0.03 0 0 0 0
50 6 0 0.03 0 0 0 0
50 8 2 0.03 27.833133599054975 0.0 1032.0 928.0
50 10 2 0.03 27.833133599054975 0.0 1032.0 928.0
46 30 2 0.17 27.833133599054975 0.0 968.0 962.0
46 32 2 0.17 27.833133599054975 0.0 1028.0 1020.0
46 34 2 0.17 27.833133599054975 0.0 978.0 1122.0
46 36 2 0.17 27.833133599054975 0.0 1000.0 1080.0
46 38 2 0.18 27.833133599054975 0.0 1010.0 1062.0
代码:
import pandas as pd
# load data as a pandas dataframe
df = pd.read_csv('dummy.txt', sep='\t', lineterminator='\r')
# to find the rows matching one condition ==2
blob2 = []
for index, row in df.iterrows():
temp = [row['Accum'], row['EdgeThr']]
if row['NumberOfBlobs']==2:
blob2.append(temp)
print(index, row['Accum'], row['EdgeThr'], row['NumberOfBlobs'])
print(blob2)
【问题讨论】:
-
嗨@JohanC 你能把它作为答案发给我吗?
标签: python pandas dataframe loops indexing