I have made a function to drop a row in a pandas.DataFrame. Since, in general, the input dataframe of a function is just a reference to the dataframe. So any change made in that function to the dataframe will be preserved outside that function. Therefore, there are two choices when designing the function:
- simply make the change without returning the dataframe.
- or, make the change and return the dataframe.
See my code below (function change_df_1
and change_df_2
). Could you please let me know which one is better or do you prefer? My concern is readability/clarity of the code.
import pandas as pd
def get_df():
df = pd.DataFrame({'A': [10, 20, 30], 'B':[100, 200, 300]})
return df
def change_df_1(df: pd.DataFrame):
df.drop(index=[1], inplace=True)
def change_df_2(df: pd.DataFrame) -> pd.DataFrame:
df.drop(index=[1], inplace=True)
return df
def main():
df = get_df()
print(f'Original {df=}')
change_df_1(df=df)
print(f'Changed_with 1: {df=}')
# change_df_2(df=df)
# print(f'Changed_with 2: {df=}')
if __name__ == '__main__':
main()
```