I have a list:
x = ['hi', 'hello', '-', '01.01.9999']
And I have a DataFrame with a lot of columns. I want to loop over all columns and count the occurrences of my custom list.
As a result, I want something like this:
column_1, 'hi', 23
column_1, 'hello', 3
column_1, '-', 5
column_1, '01.01.9999', 0
...
column_n, 'hi', 0
column_n, 'hello', 35
column_n, '-', 15
column_n, '01.01.9999', 54
Already have this:
user_selected_features['dummy_key_words'] = ['hi', 'hello', '-', '01.01.9999']
for x in user_selected_features['dummy_key_words']:
for column in _tmp_df:
I tried a lot of things in the loop, but nothing seems to return the correct result.
count = _tmp_df[_tmp_df[column] == x].count()
count = _tmp_df[column].str.count(x)
count = [_tmp_df[column] == x].count
How can I count the occurrence of a custom value per column in a DataFrame?
user_selected_featuresand_tmp_df?x = ['hi', 'hello', '-', '01.01.9999'];import string;np.random.seed(0);df = pd.DataFrame(np.random.choice(x+list(string.ascii_letters), size=100000).reshape(-1, 500),columns=[chr(i) for i in range(500)]). But as I said, it's possible one answer is better in one case and not in another one. This was the only point of my comment. Your answer is perfectly fine!