[Python][Pandas] Фильтрация кадров данных на основе значения столбцаPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 [Python][Pandas] Фильтрация кадров данных на основе значения столбца

Сообщение Anonymous »

У меня есть Excel, который мне затем нужно преобразовать в фрейм данных, а затем доработать.
Мне нужно применить следующую логику, но давайте определим несколько вещей >
Родительский узел — это узел, который начинается с WP или CH в столбце "Кодекс", но никогда не имеет "."
Каждый родительский узел имеет дочерние узлы , в формате PARENT_NODE + ".A" + номер
Если для родительского элемента значение "% di completamento" установлено на 100 %, отфильтруйте родительский + дочерние элементы
В противном случае сохраните в новый df оставшиеся родительские узлы (те, которые не 100%).
Приведенный ниже код никогда не работает правильно. Собирает готовый на 100% материал
[..] некоторый код для загрузки Excel в df (с именем df)

Код: Выделить всё

# Identify parent nodes (those that don't have a '.' in 'Codice' and do not start with 'PRO')
df['is_parent'] = ~df['Codice'].str.contains('\.', na=False) & ~df['Codice'].str.startswith('PRO', na=False)

# Identify parent nodes that are not 100% complete
incomplete_parents = df.loc[df['is_parent'] & ((df['% di completamento'] != 100)| (df['% di completamento'] != 1.00)), 'Codice']

# We want to keep rows that are children of non-complete parents or are parents themselves
# Note: It's important to ensure the 'Codice' column doesn't have NaN values for this operation
df['keep'] = df['Codice'].isin(incomplete_parents) | df['Codice'].str.contains(r'\.', na=False)

# Apply the filter to the DataFrame to keep desired rows
filtered_df = df[df['keep']].copy()

# Dropping the helper columns
filtered_df.drop(columns=['is_parent', 'keep'], inplace=True)

# Now let's display the first few rows of the dataframe to verify the output
# print (filtered_df)

columns_to_save = [
'ID',
'Codice',
'Nome dell\'attività',
'Effort stimato',
'Inizio',
'Fine',
'Durata',
'Effort consuntivato + Effort Stimato mancante', # Assuming this is the correct column name
'Diff. Effort',
'% di completamento',
'Effort completato'
]

filtered_df = filtered_df[columns_to_save].dropna()

# Now filtered_df will only include parent projects that are not 100% complete and all their child tasks
print(filtered_df)
Изображение


Подробнее здесь: https://stackoverflow.com/questions/782 ... lumn-value
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»