[英]How to add certain rows of a pandas dataframe to a list based on value of another column
我有一個 csv 文件,其中一列標記為“計數”,然后是 10 列,標記為 1-10。 總共有 100 行。 對於十列中的每一列,我想將該列中的所有值添加到列表中,其中“計數”值在 100-400 之間。 這將產生 10 個列表。 我附上了數據的樣例,以及我擁有的一些代碼。 謝謝
本質上,對於每一列,我想要該行的“計數”值在 100-400 之間的所有值的列表。 我想將所有列表保存在字典中,將列 header 映射到所需間隔內的所有值的列表。
到目前為止,我有:
import pandas as pd
dict ={}
data = pd.read_csv('Data.csv')
headers = data.columns.values
headers = headers[1:]
count = 1
for header in headers:
for index, row in data.iterrows():
dict[str(count)] = []
if 100<=data.loc[index, 'count'] <= 400:
dict[str(count)].append(data.loc[index, header])
count+=1
但這似乎在 jupyter notebook 中崩潰了。 謝謝!
從概念上講,您快到了,但您可能只需要內置的pandas
function 來幫助您執行此操作: to_dict
。
# Get the data which falls into the range of interest
range_data = data[(100<=data['count'])&(data['count']<=400)]
# Convert column names to strings (rather than numbers)
range_data.columns = range_data.columns.astype(str)
# Convert to a dictionary
value_dict = range_data.drop(columns=['counts']).to_dict(orient='list')```
您將無法在 python 中執行此操作:
100<=data.loc[index, 'count'] <= 400
您需要將每個比較分開,如下所示:
100<=data.loc[index, 'count'] and data.loc[index, 'count'] <= 400
將字典命名為dict
是個壞主意。 這將用您的字典覆蓋基本 function dict
,因此您將無法再調用dict
來創建新字典。 此外,它可能會很混亂。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.