I have data where one column is a string. This column contains text, such as:
# | financial_covenants |
---|---|
1 | Max. Debt to Cash Flow: Value is 6.00 |
2. | Max. Debt to Cash Flow: Decreasing from 4.00 to 3.00, Min. Fixed Charge Coverage Ratio: Value is 1.20 |
3 | Min. Interest Coverage Ratio: Value is 3.00 |
4 | Max. Debt to Cash Flow: Decreasing from 4.00 to 3.50, Min. Interest Coverage Ratio: Value is 3.00 |
5 | Max. Leverage Ratio: Value is 0.6, Tangible Net Worth: 7.88e 008, Min. Fixed Charge Coverage Ratio: Value is 1.75, Min. Debt Service Coverage Ratio: Value is 2.00 |
I want a new column that counts how many covenants there are in "financial_covenants". As you can see, the covenants are divided by a comma. I want my final result to look like this:
financial_covenants | num_of_cov |
---|---|
Max. Debt to Cash Flow: Value is 6.00 | 1 |
Max. Debt to Cash Flow: Decreasing from 4.00 to 3.00, Min. Fixed Charge Coverage Ratio: Value is 1.20 | 2 |
Max. Debt to Cash Flow: Value is 3.00 | 1 |
Max. Debt to Cash Flow: Decreasing from 4.00 to 3.50, Min. Interest Coverage Ratio: Value is 3.00 | 2 |
Max. Leverage Ratio: Value is 0.6, Tangible Net Worth: 7.88e 008, Min. Fixed Charge Coverage Ratio: Value is 1.75, Min. Debt Service Coverage Ratio: Value is 2.00 | 4 |
The data set is large (3000 rows), and these phrases differ among themselves in values, such like: Max. Debt to Cash Flow: Value is 3.00 and Max. Debt to Cash Flow: Value is 6.00. I am not interested in these values, but just want to know how many covenants there are.
Do you have any idea how to do this in Python?
CodePudding user response:
On the assumption that your data is in a pandas DataFrame called df with columns as labelled then you could use:
df['num_of_cov'] = df['financial_covenants'].map(lambda row : len(row.split(',')))
CodePudding user response:
Looks to me that you could use:
counts = [] # structure to store the results
for financial_covenant in financial_covenants: # your structure containing rows
count = len(financial_covenant.split(','))
counts.append(count)
print(counts)