Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python数据帧操作_Python_Pandas - Fatal编程技术网

Python数据帧操作

Python数据帧操作,python,pandas,Python,Pandas,也许我的问题太简单了,对此感到抱歉: 我有以下示例数据框(我的实际数据框有许多行和列): df JAN FEB MAR APR MAY JUN 0,1 0,1 1,3 -0,5 -0,3 0,4 -1,2 0,1 1,1 -1,2 0,4 -0,6 我正在尝试创建一个新的数据框,它的值是一个月值和3个月前的月值之间的差值。因此,特定样本数据框的输出应为: APR MAY JU

也许我的问题太简单了,对此感到抱歉:

我有以下示例数据框(我的实际数据框有许多行和列):

df

   JAN    FEB    MAR    APR    MAY    JUN 
   0,1    0,1    1,3   -0,5   -0,3    0,4
  -1,2    0,1    1,1   -1,2    0,4   -0,6
我正在尝试创建一个新的数据框,它的值是一个月值和3个月前的月值之间的差值。因此,特定样本数据框的输出应为:

    APR    MAY    JUN
   -0,6   -0,4   -0,9
     0     0,3   -1,7
因此,第一个APR值是:(-0,5-0,1)=-0,6等

我试过这个:

new_df=pd.DataFrame(0,index = df.index.values, columns = df.columns.values)

for i in list(df.index.values):
 for j in list(df.columns.values):
    new_df.iloc[i,j] = df.iloc[i,j+3] - df.iloc[i,j]
我得到这个错误:

----> 3         new_df.iloc[i,j] = df.iloc[i,j+3] - df.iloc[i,j]
TypeError: must be str, not int
有人能帮我吗?
提前感谢

在执行减法后尝试转换为字符串。也许是这样的:

for i in list(df.index.values):
    for j in list(df.columns.values):
        output =  df.iloc[i,j+3] - df.iloc[i,j]
        output = str(output)  
          new_df.iloc[i,j] = output
Months =("JAN","FEB","MAR","APR","MAY","JUN")
df = pandas.DataFrame(np.random.randn(2, 6), columns=Months).round(1)

new_df=pandas.DataFrame(0,index = df.index.values, columns = ["APR","MAY","JUN"])

for i in range(len(df.index.values)):
   for j in range(len(df.columns.values) - 3):
      new_df.iloc[i,j] = df.iloc[i,j + 3] - df.iloc[i,j]

print(new_df)
你可以做:

Months =("JAN","FEB","MAR","APR","MAY","JUN")
for i in range(3,6):
    df[Months[i]] = df[Months[i]] + df[Months[i-3]]
df.drop(columns=["JAN","FEB","MAR"])
如果你不想要开始的3个月,你可以:

Months =("JAN","FEB","MAR","APR","MAY","JUN")
for i in range(3,6):
    df[Months[i]] = df[Months[i]] + df[Months[i-3]]
df.drop(columns=["JAN","FEB","MAR"])

j
是来自此
(“一月”、“二月”、“三月”、“四月”、“五月”、“六月”)
的内容,您尝试在月份名称中添加3,但这不会发生

试试这个:

new_df=pd.DataFrame(0,index = df.index.values, columns = df.columns.values)

for i in list(df.index.values):
     for j in range(len(df.columns.values)):
        new_df.iloc[i,j] = df.iloc[i,j+3] - df.iloc[i,j]

但是您将遇到越界错误,因为
j+3
将超过列数。我不知道您想如何处理这个问题,但这就是我现在想到的。

不要使用循环,因为速度慢,如果存在矢量化解决方案:

df1 = df.sub(df.shift(3, axis=1)).iloc[:, 3:]
print (df1)
   APR  MAY  JUN
0 -0.6 -0.4 -0.9
1  0.0  0.3 -1.7
详细信息

第一个值:

然后减去:

最后通过以下方式删除第一个
3列


您应该针对熊猫的矢量化操作。下面是一个使用NumPy数组的示例:

print(df)

     JAN    FEB   MAR   APR    MAY    JUN
0 -0.000  0.400 0.200 0.200  0.900 -0.400
1 -1.100 -0.900 1.000 0.700 -0.300  0.200

df.iloc[:, 3:] -= df.iloc[:, :3].values

res = df.iloc[:, 3:]

print(res)

    APR   MAY    JUN
0 0.200 0.500 -0.600
1 1.800 0.600 -0.800

你可以这样做:

for i in list(df.index.values):
    for j in list(df.columns.values):
        output =  df.iloc[i,j+3] - df.iloc[i,j]
        output = str(output)  
          new_df.iloc[i,j] = output
Months =("JAN","FEB","MAR","APR","MAY","JUN")
df = pandas.DataFrame(np.random.randn(2, 6), columns=Months).round(1)

new_df=pandas.DataFrame(0,index = df.index.values, columns = ["APR","MAY","JUN"])

for i in range(len(df.index.values)):
   for j in range(len(df.columns.values) - 3):
      new_df.iloc[i,j] = df.iloc[i,j + 3] - df.iloc[i,j]

print(new_df)

你能提到它抛出错误的那一行吗?我已经更新了输出错误我试过了,但这并不能解决问题,我仍然得到同样的错误你能发布你的全部代码吗?您发布的代码中不存在获取错误的行。