Home > Blockchain >  Select only rows that have a value in a data range - pandas dataframe
Select only rows that have a value in a data range - pandas dataframe

Time:04-14

what i have:

         date  percentage
0  2022-04-08        20.0
1  2022-04-09         0.0
2  2022-04-10         0.0
3  2022-04-11         0.0
4  2022-04-12        10.0
5  2022-04-13         0.0
6  2022-04-14         0.0
         date  percentage
0  2022-04-08         0.0
1  2022-04-09         0.0
2  2022-04-10         0.0
3  2022-04-11         0.0
4  2022-04-12         0.0
5  2022-04-13         0.0
6  2022-04-14         0.0
         date  percentage
0  2022-04-08       100.0
1  2022-04-09         0.0
2  2022-04-10         0.0
3  2022-04-11         0.0
4  2022-04-12         0.0
5  2022-04-13         0.0
6  2022-04-14         0.0
         date  percentage
0  2022-04-08         0.0
1  2022-04-09         0.0
2  2022-04-10         0.0
3  2022-04-11         0.0
4  2022-04-12        18.0
5  2022-04-13         0.0
6  2022-04-14         0.0
         date  percentage
0  2022-04-08        70.0
1  2022-04-09         0.0
2  2022-04-10         0.0
3  2022-04-11         0.0
4  2022-04-12        77.0
5  2022-04-13         0.0
6  2022-04-14         0.0

what I expect:

         date  percentage
0  2022-04-08        20.0
1  2022-04-12        10.0
         date  percentage
0  2022-04-08       100.0
         date  percentage
0  2022-04-12        18.0
         date  percentage
0  2022-04-08        70.0
1  2022-04-12        77.0

I want to select only rows that have values in those days. remove rows that have a value of 0

I use the for loop to go through all the elements, after which I append them in a list.

CodePudding user response:

Try this:

df[df['percentage'] > 0]

CodePudding user response:

Seems need filter rows in list comprehension:

L = [df[df['percentage'].ne(0)] for df in dfs]

CodePudding user response:

To get values that are nonzero, you can simply do df = df[df["percentage"] != 0]. If your date column is a datetime data type, you can filter by days with df = df[df["date"].dt.day.isin([8, 12])]. If not and you do not want to convert it, you will need to use string slicing and it will be a bit more cumbersome.

split_date = df["date"].str.split("-", expand=True)
df = df[split_date[2].using(["08", "12"])

Where the 2 in the last command is just the last column what is returned by the splitting function.

  • Related