0

I have a data frame imported with read_csv, as in this sample, but when i use query to filter the content i get an empty result.

The expected result i'm looking to get from query is row 0 and 2.

(pandas v1.3.1, python v3.9)

df1 = pd.read_csv(r'C:\Users\Dorin\Desktop\folder_files\test_1.txt',
              encoding='utf-8',
              sep=';',
              names=["i_line", "f_path", "f_type", "f_hash"],
              dtype={'i_line': 'string', 'f_path': 'string', 'f_type': 'string', 'f_hash': 'string'},
              keep_default_na=False,
              na_values=['_'],
              index_col=False)

DataFrame print(df1)

  i_line        f_path f_type f_hash
0   i: 1   "content 1"      d    n/a
1   i: 2   "content 2"      f   1111
2   i: 3   "content 3"      d    n/a

Result of query print(df1.query("f_hash == 'n/a'"))

Empty DataFrame
Columns: [i_line, f_path, f_type, f_hash]
Index: []

File content

enter image description here

2
  • Can you post the content of the file? Is "n/a" text or the numpy.NaN object? Commented Jul 29, 2021 at 12:12
  • File content, 3 rows, content separated by semicolon i: 1; "content 1"; d; n/a; i: 2; "content 2"; f; 1111; i: 3; "content 3"; d; n/a; Commented Jul 29, 2021 at 12:26

1 Answer 1

2

In your file, the separator is not ; but rather ; (with an optional space).

Thus your n/a is in fact a n/a

You have to change the separator in read_csv:

df1 = pd.read_csv('/tmp/t.csv',
              encoding='utf-8',
              sep='; ?',  ## sep is ";" with optional space
              names=["i_line", "f_path", "f_type", "f_hash"],
              dtype={'i_line': 'string', 'f_path': 'string', 'f_type': 'string', 'f_hash': 'string'},
              keep_default_na=False,
              na_values=['_'],
              index_col=False)
Sign up to request clarification or add additional context in comments.

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.