1

I have a csv file bellow:

df.csv

Symbol  Total Volume (on 01/17/2018)
A   
B   
C       1.900
D   
E   
F   
G       1.051

I would like to find the Symbols if the Total Volume (on 01/17/2018) value is not null. In another words, I would like to retung "G", in this case.

I was tryng to do it with those lines of code:

df = pd.read_csv('./df.csv')
type_filter = df[df['Total'].str.match('', case=False)]
    a = type_filter['Symbol']
print type_filter

Any ideas?

By the way, could you also give me an example if I would like to find the Symbols if the Total value is null?

4
  • 1
    df.Symbol[df.Total.str.strip().ne('')] And for the inverse, invert the condition with ~. Commented Jan 21, 2018 at 10:56
  • I've edited the question, could you check it again, and see if this works? Commented Jan 21, 2018 at 11:00
  • For your edit, it should return C and G, right? Commented Jan 21, 2018 at 11:04
  • yes, it should. Commented Jan 21, 2018 at 11:19

2 Answers 2

1

You can use str.strip with a not equals comparison with ne, and use the resultant mask to index df -

df

  Symbol Total Volume
0      A             
1      B             
2      C        1.900
3      D             
4      E             
5      F             
6      G        1.051

df.loc[df['Total Volume'].str.strip().ne(''), 'Symbol']

Or, if you have a numeric column with NaNs -

df.loc[df['Total Volume'].notnull(), 'Symbol']

2    C
6    G
Name: Symbol, dtype: object
Sign up to request clarification or add additional context in comments.

12 Comments

it is returning this: AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas
BTW, when I print(df) the empty cells return "NaN"
What if I would like to look in two columns, what should I do?
@rafasalo df.loc[df['Total Volume'].notnull(), ['col1', 'col2']]
Sorry, I didn't explain right. If I have a third column. Like: Symbol, Total Volume and Percentage, and I want to look at notnull() in Total Volume and Percentage?
|
0

I found how:

df = pd.read_csv('./df.csv')
type_filter = df[df['Total'].notnull()]
a = type_filter['Symbol']
print type_filter

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.