Reputation: 47
list2 = [0, 6, 91, 99, 105, 586]
data1 = pd.read_csv("C:\\Users\\HR085368\\Downloads\\2-8-21\\df_live.csv", index_col=0)
df_new[data1.index.isin(list2)]
So (data1.index) is a column in the csv file. Im trying to select only the index from list2 into a new data frame df_new, but Im getting this error
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-151-c19d659a20f5> in <module>
----> 1 df_new[data1.index.isin(list2)]
c:\users\hr085368\appdata\local\programs\python\python39\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
3013 # Do we have a (boolean) 1d indexer?
3014 if com.is_bool_indexer(key):
-> 3015 return self._getitem_bool_array(key)
3016
3017 # We are left with two options: a single key, and a collection of keys,
c:\users\hr085368\appdata\local\programs\python\python39\lib\site-packages\pandas\core\frame.py in _getitem_bool_array(self, key)
3060 )
3061 elif len(key) != len(self.index):
-> 3062 raise ValueError(
3063 f"Item wrong length {len(key)} instead of {len(self.index)}."
3064 )
ValueError: Item wrong length 708 instead of 0.
Upvotes: 1
Views: 1466
Reputation: 863226
You need filter same DataFrame called data1
:
data1[data1.index.isin(list2)]
If all values exist is possible use:
data1.reindex(list2)
Upvotes: 1