Home:ALL Converter>python string manipulation with pandas

python string manipulation with pandas

Ask Time:2022-04-19T18:03:35         Author:Chris

Json Formatter

I'm trying to do some string manipulations with Pandas and I would deeply appreciate your help! Here's my problem: I loaded a list of words from a csv file into a pandas dataframe called df, so that it looks as follows (here, I created the df manually):

data = {'Keyword': ['Apple', 'Banana', 'Peach', 'Strawberry', 'Blueberry'], 'Kategory': ['A', 'A', 'A', 'B', 'B']}  

df = pd.DataFrame(data) 

Now what I would like to do is some string manipulation based on the following conditions shown below. The output of the string manipulation should be saved to a new column.

# new column to store the results
output = []

# set up the conditions
for Keyword in df:
    if df[Kategory] == 'A':
        output.append(Keyword + 'first choice')
        print(Keyword + 'first choice')
    else:
        output.append(Keyword + 'second choice')        
        print(Keyword + 'second choice') 

Thank you very much for your help!!

Author:Chris,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/71923121/python-string-manipulation-with-pandas
Goutam Kumar Jha :

data = {'Keyword': ['Apple', 'Banana', 'Peach', 'Strawberry', 'Blueberry'], 'Kategory': ['A', 'A', 'A', 'B', 'B']} \n\ndf = pd.DataFrame(data) \noutput = []\nfor idx, rows in df.iterrows():\n if rows['Kategory'] == 'A':\n output.append(rows['Keyword'] + " "+'first choice')\n # print(Keyword + 'first choice')\n else:\n output.append(rows['Keyword']+ " "+ 'second choice') \n # print(Keyword + 'second choice') \n\ndf['output'] = output\nprint(df)\n\nKeyword Kategory output\n0 Apple A Apple first choice\n1 Banana A Banana first choice\n2 Peach A Peach first choice\n3 Strawberry B Strawberry second choice\n4 Blueberry B Blueberry second choice\n\n\n\nI have tried to replicate your approach , but you can use np.where , to iterate on a dataframe you have to use index and rows",
2022-04-19T10:35:45
Ynjxsjmh :

You can try np.where\ndf['col'] = np.where(df['Kategory'].eq('A'), df['Keyword'].add(' first choice'), df['Keyword'].add(' second choice'))\n\nprint(df)\n\n Keyword Kategory col\n0 Apple A Apple first choice\n1 Banana A Banana first choice\n2 Peach A Peach first choice\n3 Strawberry B Strawberry second choice\n4 Blueberry B Blueberry second choice\n",
2022-04-19T10:26:19
yy