我编写了这个脚本,可以根据满足两个条件的值创建新列。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df=pd.DataFrame()
df['variable 1']= np.arange(0,1.1,0.1)
df['variable 2']= 0.2*df['variable 1']
df['variable 3']= 0.4 -0.2*df['variable 1']
# Create new columns
slope = [2, 1.5, 1, 0.5]
for i in range(len(slope)):
df['slope = ' + str(slope[i])]=''
for j in range(len(df['variable 1'])):
# Calculating Scl_disp_sd with equation 1
curve = 0.5 - slope[i]*df['variable 1'][j]
df['slope = ' + str(slope[i])][j]= np.where((curve>df['variable 2'][j]) & (curve<df['variable 3'][j]), curve,np.nan)
display(df)
plt.plot(df['variable 1'], df['variable 2'], 'o', label='variable 2')
plt.plot(df['variable 1'], df['variable 3'], 'o', label='variable 3')
plt.plot(df['variable 1'], df.filter(like='slope =', axis=1), marker='.')
plt.legend()
该脚本有效,但是我收到以下消息:
/var/folders/m0/_y1fs5x50xx99pjg2yf42y7r0000gp/T/ipykernel_1964/2618301266.py:11: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0!
You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.
A typical example is when you are setting values in a column of a DataFrame, like:
df["col"][row_indexer] = value
Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df['slope = ' + str(slope[i])][j]= np.where((curve>df['variable 2'][j]) & (curve<df['variable 3'][j]),
/var/folders/m0/_y1fs5x50xx99pjg2yf42y7r0000gp/T/ipykernel_1964/2618301266.py:11: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df['slope = ' + str(slope[i])][j]= np.where((curve>df['variable 2'][j]) & (curve<df['variable 3'][j]),
/var/folders/m0/_y1fs5x50xx99pjg2yf42y7r0000gp/T/ipykernel_1964/2618301266.py:11: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0!
You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.
A typical example is when you are setting values in a column of a DataFrame, like:
df["col"][row_indexer] = value
Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
...
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df['slope = ' + str(slope[i])][j]= np.where((curve>df['variable 2'][j]) & (curve<df['variable 3'][j]),
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
如果有人能有其他想法来编写此脚本以避免出现此消息,我将不胜感激
不需要嵌套循环。只需将您的操作作为向量应用即可:
或者使用numpy的完整矢量:
输出: