假設我們有一個具有三個特徵的熊貓數據框,如下所示。pandas dataframe獲取行號並添加到列表
每個行是代表客戶和代表該客戶的一些特徵的每一列。
我想獲得行號並將它們添加到列表中,或根據其特徵值將它們添加到列表中。
比方說,我們希望找到的行數,如果FEATUREA小於100或FEATUREB超過500
我已經寫了一些代碼,這是你可以看到下面。
import pandas as pd
d = [{'feature1': 100, 'feature2': 520, 'feature3': 54},
{'feature1': 102, 'feature2': 504, 'feature3': 51},
{'feature1': 241, 'feature2': 124, 'feature3': 4},
{'feature1': 340, 'feature2': 830, 'feature3': 700},
{'feature1': 98, 'feature2': 430, 'feature3': 123}]
df = DataFrame(d)
print(df)
print("----")
dataframe1 = df[(df['feature1'] < 100)]
dataframe2 = df[(df['feature2'] > 500)]
print(dataframe1)
print(dataframe2)
# here I would like to get row number temp and add them to result list
輸出程序
feature1 feature2 feature3
0 100 520 54
1 102 504 51
2 241 124 4
3 340 830 700
4 98 430 123
----
feature1 feature2 feature3
4 98 430 123
feature1 feature2 feature3
0 100 520 54
1 102 504 51
3 340 830 700
我無法弄清楚如何dataframe1和dataframe2結合,然後得到他們的行號。如果你知道如何去做,你能分享一下嗎?
我希望看到這樣的
result = [ 4, 0, 1, 3]
的可能的複製[Python的熊貓:獲取所有列匹配一定值的行指數](https://stackoverflow.com/questions/21800169/python-pandas-get-index-of-rows-which- column-matches-certain-value) – Mako212
通過上面的鏈接:'df.index [(df ['feature2']> 500)]。tolist()' – Mako212
我編輯了這個問題。我也想弄清楚如何組合這兩個數據集。 – wasabi