Home > front end >  edit pd Dataframe through iteration
edit pd Dataframe through iteration

Time:12-19

I have a DataFrame of Series created by pandas.read_html().

I'm trying to run drop() on each Series in each iterrow() label of the dataframe with the code below:

df = pd.read_html('http://openinsider.com')
df = df[-3]
rm = ['1m', '1w', '1d', '6m']
for index, label in df.iterrows():
    print(label)
    label.drop(index=rm, inplace=True)
    label.dropna(inplace=True)
    print(label)

I can see the values being removed in the print statements, but the dataframe is unchanged.

CodePudding user response:

Do you want:

df = df[df.columns.difference(rm)]
print(df)

# Output:
               Company Name          Filing Date                  Insider Name     Owned    Price      Qty Ticker                  Title  Trade Date    Trade Type        Value    X   ΔOwn
0              Airbnb, Inc.  2021-12-17 21:59:52              Jordan Jeffrey D    322696  $166.25   -10000   ABNB                    Dir  2021-12-16      S - Sale  -$1,662,500  NaN    -3%
1              Airbnb, Inc.  2021-12-17 21:57:59            Johnson Belinda J.    199327  $166.25   -20000   ABNB                    Dir  2021-12-16   S - Sale OE  -$3,325,000    D    -9%
2        Venus Concept Inc.  2021-12-17 21:45:12  Ew Healthcare Partners, L.P.  32043822    $1.25  3920000   VERO                    10%  2021-12-15  P - Purchase   $4,900,000  NaN    14%
3        Venus Concept Inc.  2021-12-17 21:31:06           Barry Richard Scott  16337856    $1.25  3920000   VERO               Dir, 10%  2021-12-15  P - Purchase   $4,900,000  NaN    32%
4  Hims & Hers Health, Inc.  2021-12-17 21:28:10                Becklund Irene     25587    $6.35    -4509   HIMS       Interim PFO, PAO  2021-12-15       F - Tax     -$28,632    D   -15%
5  Hims & Hers Health, Inc.  2021-12-17 21:27:54                  Dudum Andrew  11409129    $6.35   -22053   HIMS               CEO, 10%  2021-12-15       F - Tax    -$140,037    D     0%
6  Hims & Hers Health, Inc.  2021-12-17 21:27:36               Boughton Soleil    389715    $6.35    -5255   HIMS                     GC  2021-12-15       F - Tax     -$33,369    D    -1%
7  Hims & Hers Health, Inc.  2021-12-17 21:27:20                 Baird Melissa    257681    $6.35    -9572   HIMS                    COO  2021-12-15       F - Tax     -$60,782    D    -4%
8  Hims & Hers Health, Inc.  2021-12-17 21:26:34      Carroll Patrick Harrison     20857    $6.35    -1368   HIMS  Chief Medical Officer  2021-12-15       F - Tax      -$8,687    D    -6%
9              Ocugen, Inc.  2021-12-17 21:24:13                  Potti Manish     72695    $1.43    45000   OCGN                    Dir  2021-12-16     M - OptEx      $64,530    D   162%
  • Related