Changing Value in Data Frame Column in a Loop Python

Changing value in data frame column in a loop python

You can use just this:

df_merged['new_value'] = df.index

You can also use apply method.

df_merged['new_value'] = df_merged.apply(lambda row : row.name, axis=1)

I am getting this error : A value is trying to be set on a copy of a
slice from a DataFrame

It's not a error, it's just a warning message.

From this answer:

The SettingWithCopyWarning was created to flag potentially confusing "chained" assignments, such as the following, which don't always work as expected, particularly when the first selection returns a copy.

You can avoid this warning message using pd.DataFrame.loc method.

for i, row in df_merged.iterrows():
df_merged.loc[i,'price_new'] = i

A simple way to replace all column values in a loop of a dataframe according to another column

You can just use the apply function as follows:

df = pd.DataFrame([['q1','red','blue','grey','green','opt1'],
['q2','red','blue','grey','green','opt3']],
columns=['ques','opt1','opt2','opt3','opt4','correctOpt'])

df.apply(lambda row: row['opt'+row['correctOpt'][3]], axis=1)

The apply function applies a custom function to each row in a DataFrame. 'axis=1' gives the custom function the DataFrame ROW as an argument (instead of column), then row['correctOpt'][3] gets the correct option number, and row['opt'+row['correctOpt'][3]] gets the result from the opt column.

Loop over dataframe and change value by compared to the previous line value

IIUC your code, you can use rolling:

ffill = lambda x: pd.DataFrame(x.rolling('30T', on='date')['value_dex'].max(),
index=x.index)

df['value_dex'] = df.groupby('id', as_index=False).apply(ffill)
print(df)

# Output
date id value_dex
90256 2021-05-10 01:45:20 101904285 7.6
90257 2021-05-10 01:45:20 101904285 7.6
90258 2021-05-10 02:00:00 101904285 7.6
90259 2021-05-10 02:00:44 101904285 7.6
90260 2021-05-10 02:00:44 101904285 7.6
90261 2021-05-10 02:00:44 101904285 7.6
90262 2021-05-10 02:21:51 101904285 7.6
90263 2021-05-10 02:21:51 101904285 7.6
90264 2021-05-10 02:21:51 101904285 7.6
90265 2021-05-10 02:21:51 101904285 7.6
90266 2021-05-10 03:00:00 101904285 NaN
90267 2021-05-10 03:00:44 101904285 12.0
90268 2021-05-10 03:00:44 101904285 12.0
90269 2021-05-10 03:00:44 101904285 12.0

Dataframe doesn't modify in a for loop

Try without the loc.

for col in last_df:
last_df[last_df[col]!=0]='stop'
stop=last_df[last_df[col]=='stop'][col].index[0]
last_df[col][:(stop-1)]='NaN'

this is a issue with view and copy of the DataFrame for more information quick understand you can read in this link

For loop with if to detect variable change in dataframe columns

I believe that @Rashan Arshad has identified the issue exactly. In the second for loop, the program will assign data = df.iloc[i,j] for each value of i, but it will not check if data is equal to 1 until it has exited the for loop. This is why you only flag this column if the last entry is 1.

The fix is indentation of the if-else conditional.

However, to address the comment below, we actually want to refactor slightly, so that we only have to iterate through each column, rather than every column and row:

import tkinter as tk
from tkinter import filedialog
import matplotlib.pyplot as plt
import pandas as pd

def get_csv(): # Import data from a csv file
global df
import_file_path = filedialog.askopenfilename(filetypes=[("CSV file", '*.csv')])
df = pd.read_csv(import_file_path, sep=';', header=0, na_values=['#NV', ' '], decimal=',', encoding='cp1252')
return df


def loop_column():
# fault = []
name_list = []
name_list2 = []
for j in range(164, 212): # cycle through column 165 to 211 of dataframe
# fault.append(j)
col_name = df.columns[j]
if 1 in df[col_name].values:
name_list.append(col_name)
print(col_name + " = 1")
else:
name_list2.append(col_name)
print(col_name + " = 0")
print(name_list)
print(name_list2)


get_csv()
loop_column()
root.mainloop()


Related Topics



Leave a reply



Submit