[英]Multiple for loops to create a pandas dataframe
I am trying to create a pandas dataframe that looks like this: 我正在尝试创建一个如下所示的熊猫数据框:
-5 0 5
index
-5 NaN slope slope
0 slope NaN slope
5 slope slope NaN
but the closest I can get is the code below which returns a dataframe with only one column (which is the list from the last iteration through the ctr1 loop) 但是我能得到的最接近的代码是下面的代码,该代码返回仅包含一列的数据帧(这是从最后一次迭代到ctr1循环的列表)
weather = np.linspace(-5, 5, 3)
for ctr1 in weather:
slope_list = []
X1 = round(ctr1,1)
for ctr2 in weather:
X2 = round(ctr2,1)
Y1 = regressor[0] * X1**3 + \
regressor[1] * X1**2 + \
regressor[2] * X1 + \
regressor[3]
Y2 = regressor[0] * X2**3 + \
regressor[1] * X2**2 + \
regressor[2] * X2 + \
regressor[3]
slope = (Y2-Y1)/(X2-X1)
slope_list.append(slope)
df_final = pd.DataFrame({X1:slope_list})
Can anyone help? 有人可以帮忙吗?
df_final is getting only 3 elements because it's at the same indentation level as for ctr2 in weather
, so it's getting reassigned every outer loop. df_final仅获得3个元素,因为它与for ctr2 in weather
缩进级别相同,因此每个外部循环都将其重新分配。 Although, if you fix that, you'll get a dataframe that's only a single long column: you only have a single slope_list
getting appended to that turns into a dataframe at the end. 虽然,如果您修复此问题,则将获得仅是一个长列的数据slope_list
:仅将一个slope_list
附加到该数据slope_list
的末尾。
This is how I would solve that without changing your assignment method: 这是我不更改分配方法即可解决的方法:
weather = np.linspace(-5, 5, 3)
slope_list = []
for ctr1 in weather:
X1 = round(ctr1,1)
for ctr2 in weather:
X2 = round(ctr2,1)
Y1 = regressor[0] * X1**3 + \
regressor[1] * X1**2 + \
regressor[2] * X1 + \
regressor[3]
Y2 = regressor[0] * X2**3 + \
regressor[1] * X2**2 + \
regressor[2] * X2 + \
regressor[3]
slope = (Y2-Y1)/(X2-X1)
slope_list.append(slope)
#make it 3 columns and 3 rows as intended
slope_list = np.array(slope_list).reshape(3, 3)
#make the dataframe
df_final = pd.DataFrame({X1:slope_list})
#manually add the desired row and column indexes
df_final = df.set_index(weather)
df_final.columns = weather
Although you should keep in mind that unless you know exactly what you're doing, making loops and nested loops when working with pandas usually means you're missing a much easier and better way to go about things. 尽管您应该记住,除非您确切地知道自己在做什么,否则在处理熊猫时进行循环和嵌套循环通常意味着您会错过一种更轻松,更好的处理方法。
You can try directly assign values in DataFrame. 您可以尝试直接在DataFrame中分配值。 Just create empty DataFrame with index=weather: 只需使用index = weather创建空的DataFrame:
import numpy as np
weather = np.linspace(-5, 5, 3)
df_final = pd.DataFrame([], index=weather)
for ctr1 in weather:
X1 = round(ctr1,1)
for ctr2 in weather:
X2 = round(ctr2,1)
Y1 = regressor[0] * X1**3 + \
regressor[1] * X1**2 + \
regressor[2] * X1 + \
regressor[3]
Y2 = regressor[0] * X2**3 + \
regressor[1] * X2**2 + \
regressor[2] * X2 + \
regressor[3]
slope = (Y2-Y1)/(X2-X1)
df_final.loc[X1, X2] = np.NaN if X1 == X2 else slope
slope_list = []
resets resulting list at each iteration, so just the last one remains. slope_list = []
在每次迭代时重置结果列表,因此仅剩下最后一个。 You need to define result list outside the outer loop, and append subresults to it. 您需要在外部循环之外定义结果列表,并将子结果附加到该列表中。
As mentioned, for completeness, I have posted an answer to my question that works with a larger weather array. 如前所述,出于完整性考虑,我发布了我的问题的答案,该问题适用于更大的天气范围。 The only difference is that I did the rounding earlier in the code: 唯一的区别是,我在代码前面做了四舍五入:
weather = np.round(np.linspace(-5, 35, 401), decimals = 1)
df_final = pd.DataFrame([], index=weather)
for ctr1 in weather:
X1 = ctr1
for ctr2 in weather:
X2 = ctr2
Y1 = regressor[0] * X1**3 + \
regressor[1] * X1**2 + \
regressor[2] * X1 + \
regressor[3]
Y2 = regressor[0] * X2**3 + \
regressor[1] * X2**2 + \
regressor[2] * X2 + \
regressor[3]
slope = (Y2-Y1)/(X2-X1)
df_final.loc[X1, X2] = np.NaN if X1 == X2 else slope
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.