Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Drop rows from data with zero value
#1
I know it's probably a real simple answer... I only have 2 columns of data, 1 being the Timedate Index, and the 2nd is my values. Most of the values are actually ZERO... I'm wanting to drop all rows with a ZERO value so it is only analyzing data that has a value. I'm running it through a percent change function looking for percent change between all non-zero data... I MUCH APPRECIATE any help
Quote
#2
Line 12 below will do it for you.

data = [
    ["2018-01-01", 0],
    ["2018-01-02", 1],
    ["2018-01-03", 0],
    ["2018-01-04", 0],
    ["2018-01-05", 0],
    ["2018-01-06", 31],
    ["2018-01-07", 0],
    ["2018-01-08", 0]
]

data2 = [entry for entry in data if entry[1] != 0]
print(data2)
Quote
#3
If stullis' code doesn't answer your question, I highly recommend providing your code so that we have some context about what you're working with. Ideally it would be minimal code (5-10 lines) that shows the problem.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#4
(Dec-27-2018, 12:15 AM)micseydel Wrote: If stullis' code doesn't answer your question, I highly recommend providing your code so that we have some context about what you're working with. Ideally it would be minimal code (5-10 lines) that shows the problem.

It actually didn't work... maybe it's because my data actually has headers... here is my basic code

import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import os
import io

# import 
df = pd.read_csv ('NQH7.txt', sep=', ', engine='python') #FOR ME it is importing the file with a space after the comma so having to specify that it is more than just a comma...
                                        # Without specifying it produces column names that begin with a space and that creates issues all over the place
# clean whitespaces if they exist 
df = df.applymap(lambda x: x.strip() if isinstance(x, str) else x)

# combine date and time into single column
df['datetime'] = pd.to_datetime(df.Date + ' ' + df.Time, format="%Y/%m/%d %H:%M:%S.%f")
df.set_index("datetime", inplace = True)
df = df.drop(columns=['Date', 'Time', 'Open', 'High', 'Low', 'Last', '# of Trades', 'Volume', 'OHLC Avg', 'HLC Avg', 
                     'HL Avg', 'Bid Volume', 'Ask Volume', 'Zig Zag Mid-Point', 'Extension Lines']) # drop combined columns
When I print I get...
Output:
Zig Zag Text Labels datetime 2017-01-02 17:00:00.000 5026.25 10.0 2017-01-02 17:00:00.220 5027.66 0.0 2017-01-02 17:00:00.400 5029.07 0.0 2017-01-02 17:00:07.100 5030.48 0.0 2017-01-02 17:00:08.700 5031.89 0.0 2017-01-02 17:00:08.170 5033.30 0.0 2017-01-02 17:00:09.000 5034.70 0.0 2017-01-02 17:00:11.400 5036.11 0.0 2017-01-02 17:00:18.500 5037.52 0.0 2017-01-02 17:00:22.200 5038.93 0.0 2017-01-02 17:17:08.000 5040.34 0.0 2017-01-02 17:19:45.700 5041.75 12.0 2017-01-02 18:02:02.100 5040.22 0.0 2017-01-02 18:04:02.300 5038.69 0.0 2017-01-02 18:05:57.000 5037.16 0.0 2017-01-02 18:14:54.000 5035.63 0.0 2017-01-02 18:18:41.300 5034.09 0.0 2017-01-02 18:35:01.100 5032.56 0.0 2017-01-02 18:36:25.000 5031.03 0.0 2017-01-02 18:37:25.000 5029.50 3.0 2017-01-02 18:45:40.000 5035.75 1.0
I'm wanting to basically append the df or create a new one that drops all rows where the 3rd column is a ZERO

THANKS BUT I GOT IT WORKING!!!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Filter rows by multiple text conditions in another data frame i.e contains strings an Pan 0 262 Jun-09-2020, 06:05 AM
Last Post: Pan
  How can I convert time-series data in rows into column srvmig 0 187 Apr-11-2020, 05:40 AM
Last Post: srvmig
  Panel Data Cleaning Drop Identity luxlambo 1 203 Jan-13-2020, 09:55 PM
Last Post: jefsummers
  drop rows that doesnt matched karlito 6 611 Oct-28-2019, 12:21 PM
Last Post: karlito
  Drop rows if a set of columns has a value dervast 1 404 Sep-12-2019, 04:18 PM
Last Post: sd_0912
  Creating new rows and adding them to empty data frame kapilan15 0 506 May-31-2019, 10:19 AM
Last Post: kapilan15
  How to filter specific rows from large data file Ariane 7 4,117 Jun-29-2018, 02:43 PM
Last Post: gontajones
  how to select particular rows data from a array raady07 3 1,877 Mar-06-2017, 02:21 AM
Last Post: raady07

Forum Jump:


Users browsing this thread: 1 Guest(s)