Home > database >  How to make a function with if condition for pandas dataframe
How to make a function with if condition for pandas dataframe

Time:12-14

I want to make a function that can append a new line of text to a .txt file with if condition. I'm not good at code. Can someone explain to me how to create a function in python?

ex : given a table like this

text similarity polarity
ABCDD 0.6 POSITIF
ASDAS 0.4 NEGATIF
ASGAS 1.0 POSITIF

So , i want to make a function with if condition. The condition is like this : If the value in column dataframe ['similarity'] is greater than 0.5 , and the value in other column dataframe ['polarity'] is "POSITIF". Then , i want to append a new line of text to a .txt file. Can someone explain how to make that possible? I've tried myself , but i guess it's a total mess

if (df['similarity'] > 0.5) & df['polarity'].str.contains('POSITIF').any():
    with open("text.txt","a") as myfile:
        myfile.write("text appended \n")

CodePudding user response:

It depends what you want to do exactly.

If you need to loop over all the rows, useiterrows (Warning, it is slow!):

with open("text.txt","a") as myfile:
    for key, row in df.iterrows():
        if (row['similarity'] > 0.5) and ('POSITIF' in row['polarity']):
            myfile.write("text appended \n")

If you want to append the line if any row matches both conditions:

if (df['similarity'].gt(0.5) & df['polarity'].str.contains('POSITIF')).any():
    with open("text.txt","a") as myfile:
        myfile.write("text appended \n")

CodePudding user response:

To create a function that includes an if condition for a pandas DataFrame, you can use the following steps:

  1. Import the pandas library and create a DataFrame
  2. Define a function that takes a DataFrame as an input
  3. Use the if statement to check a condition on the DataFrame
  4. Perform the desired operation on the DataFrame, such as selecting specific columns or rows based on the condition
  5. Return the modified DataFrame

Here is an example of how you might create a function that filters a DataFrame to only include rows where a specific column has a value greater than a certain threshold:

import pandas as pd

# create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

def filter_df(df):
  # check if the value in column 'A' is greater than 2
  if df['A'] > 2:
    # filter the DataFrame to only include rows where column 'A' is greater 
 than 2
    df = df[df['A'] > 2]
  return df

# apply the function to the DataFrame
filtered_df = filter_df(df)

# display the filtered DataFrame
print(filtered_df)

In this example, the function filter_df() takes a DataFrame as input and uses an if statement to check if the values in column 'A' are greater than 2. If the condition is true, the function filters the DataFrame to only include rows where column 'A' is greater than 2. The modified DataFrame is then returned.

  • Related