可以通过以下步骤实现:
np.random.choice
函数生成随机索引,以便删除随机行:random_index = np.random.choice(df[condition].index)drop
函数删除随机行:df = df.drop(random_index)完整的代码如下:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, 2, 3, 4, 5],
'B': ['a', 'b', 'c', 'd', 'e'],
'C': [True, False, True, False, True]})
condition = df['C'] == True
random_index = np.random.choice(df[condition].index)
df = df.drop(random_index)
这样就可以根据条件从dataframe中随机删除一行。请注意,这只是一个示例代码,实际应用中可能需要根据具体情况进行调整。
领取专属 10元无门槛券
手把手带您无忧上云