I have a some dataframes whose columns have string values (sentences). each of these dataframes have column names that either has the word 'gold' in combination with other words (e.g, df.columns: 'gold_data', 'dataset_gold',...etc' or has the word 'labeled' in combination with other words (e.g, df.columns: 'labeled_data', 'dataset_labeled',...etc' or have both 'gold' and 'labeled' in combination with other words.
Here is an example of how the dataframes look like if both column name exists.
import pandas as pd
df = pd.DataFrame({'gold_data':['hello the weather nice','this is interesting','the weather is good'],
'data2':['goodbye','the plant is green','the weather is sunny'],
'new_labeled_dataset':['hello','there is no food in the fridge','this weather amazing']})
I trying to process the strings in the columns depending on which one exists and return the dataframe where the conditions were true for the rows in the original dataframe as follows.
result = []
for index, entry in df.iterrows():
if not any(df.columns.str.contains(pat='labeled')):
text = entry.filter(regex='gold').squeeze()
else:
text = entry.filter(regex='labeled').squeeze()
if len(text.split()) > 2:
# assigment? = 'new_info:' + text (this is where i do not know how to assign back to the column which was processed)
result.append(entry)
print(pd.DataFrame(result))
so, I am saying if there is no 'labeled' in column names take the text from the column that has the word 'gold' otherwise take the text from 'labeled' column. But since I do not know the complete name of the column, i am not sure how to assign the processed text back to that column. The desired output should be:
gold_data data2 augmented_new
0 new_info:this is interesting the plant is green there is no food in the fridge
1 new_info:the weather is good the weather is sunny this weather amazing
I have tried to get the full_name of the column and assign it to that, but that is not correct either.
# df[col for col in df if 'gold' or 'labeled' in col] ='new_info:' + text