This question is exactly as the following request, with one more twist,
- Pandas: Replacing column values in dataframe
- Conditional Substitution of values in pandas dataframe columns
So, I want to set, or conditionally set pandas dataframe column values. The added complexity is, instead of addressing the dataframe columns with string constant (df['data1']
), I need to address them with variables (df[var_for_data1]
), becaus my df
column names are constructed.
Here is the much simplified example to explain what I want:
df = pd.DataFrame({'data1': np.random.randn(100),'data2': np.random.randn(100)})
print(df.head())
Col = 'data1'
print(df[Col].head())
df.data1 = df.data1 +.1
print(df[Col].head())
# so far so good, now how to do above with variable dataframe column name `Col`
#df.Col = df.Col + .1
The question is in the code, so far so good, now how to do above with variable dataframe column name Col
.
The next question is how to add a condition to the above assignment, say to do it if df.data1 >=.25 and df.data1 <= .35:
. Of course, expressing it using the variable dataframe column name Col
.