Я написал код для извлечения индекса из фрейма данных, но я не знаю, как использовать эти индексы для создания другого фрейма данных из исходного фрейма данных.
Можно ли также сократить мой текущий код?Это довольно долго.
EDITED ==
import pandas as pd
a = pd.DataFrame({"a":["I have something", "I have nothing", "she has something", "she is nice", "she is not nice","Me", "He"],
"b":[["man"], ["man", "eating"], ["cat"], ["man"], ["cat"], ["man"], ["cat"]]})
a = a[a.b.apply(lambda x:len(x)) == 1] # is it possible to shorten the code from here
c = a.explode("b").groupby("b")
k = ["man", "cat"]
bb = a
for x in k:
bb = c.get_group(x).head(2).index # to here?.... this part is supposed to take the first 2 indexes of each element in k
Текущие результаты:
a b
4 she is not nice [cat]
Expected results:
a b
0 I have something [man]
2 she has something [cat]
3 she is nice [man]
4 she is not nice [cat]