Python 将多个数据帧除以一个标量

Python 将多个数据帧除以一个标量,python,pandas,dataframe,Python,Pandas,Dataframe,我有几个数据帧,我想除以一个标量。将该操作一次应用于单个数据帧成功地更改了数据帧,但尝试编写for循环来执行相同的操作不会导致数据帧的更改。为什么会这样?如何编写以下循环来更改数据帧 # List of dataframes dflist = [df1,df2,df3] for i in dflist: i = i/1000 # This loop does not affect the dataframes df1=df1/1000 df2=df2/1000 df3=df3/10

我有几个数据帧,我想除以一个标量。将该操作一次应用于单个数据帧成功地更改了数据帧,但尝试编写for循环来执行相同的操作不会导致数据帧的更改。为什么会这样?如何编写以下循环来更改数据帧

# List of dataframes
dflist = [df1,df2,df3]

for i in dflist:
    i = i/1000 # This loop does not affect the dataframes

df1=df1/1000 
df2=df2/1000
df3=df3/1000 # These commands do affect the dataframes

这是因为在
for
循环实现中,您正在更新
i
,而不是列表本身的元素。将循环的
更改为以下内容应该可以工作:

dflist = [df1,df2,df3]

for i in range(len(dflist)):
    dflist[i] = dflist[i]/1000
尽管这仍然不会更新dfi,因为它将用新元素替换列表中的元素。另一种方法是:

df1, df2, df3 = [df/1000 for df in [df1,df2,df3]]

这是因为在
for
循环实现中,您正在更新
i
,而不是列表本身的元素。将
循环的
更改为以下内容应该可以工作:

dflist = [df1,df2,df3]

for i in range(len(dflist)):
    dflist[i] = dflist[i]/1000
尽管这仍然不会更新dfi,因为它将用新元素替换列表中的元素。另一种方法是:

df1, df2, df3 = [df/1000 for df in [df1,df2,df3]]

您正在寻找的是一种在适当的位置进行分区的方法。据我所知,pandas不直接支持这一点,但有一种方法可以解决这一问题——将其分配给所有列,而不是数据帧

df1 = pd.DataFrame(np.arange(0, 50).reshape(10, 5))
df2 = pd.DataFrame(np.arange(50, 100).reshape(10, 5))
df3 = pd.DataFrame(np.arange(100, 150).reshape(10, 5))

df_list = [df1, df2, df3]

for df in df_list: 
    df[df.columns] = df[df.columns] / 10
如果您
打印(df1)
,您将获得:

     0    1    2    3    4
0  0.0  0.1  0.2  0.3  0.4
1  0.5  0.6  0.7  0.8  0.9
2  1.0  1.1  1.2  1.3  1.4
3  1.5  1.6  1.7  1.8  1.9
4  2.0  2.1  2.2  2.3  2.4
5  2.5  2.6  2.7  2.8  2.9
6  3.0  3.1  3.2  3.3  3.4
7  3.5  3.6  3.7  3.8  3.9
8  4.0  4.1  4.2  4.3  4.4
9  4.5  4.6  4.7  4.8  4.9
关于就地与非就地操作的注意事项 根据评论中的一个问题,我将尝试解释我所说的就地操作是什么意思。基本上,就地操作会更改对象本身。非在位操作将创建新对象。下面的代码说明了这一点:

df1 = pd.DataFrame(np.arange(0, 6).reshape(3, 2))
print(df1) => results in 
   0  1
0  0  1
1  2  3
2  4  5

print(id(df1))
4880614608 ==> this is the id (address) of the original df1

df1 = df1 / 10 ==> this produces a __new__ dataframe. 
print(id(df1))
==> 4880613520 # note that this id is different than the previous one. 


df1[df1.columns] = df1[df1.columns] * 100
print(df1) ==> this updates df1, and it now contains: 
      0     1
0   0.0  10.0
1  20.0  30.0
2  40.0  50.0

print(id(df1)) ==> The id / address of df _did_ _not_ change: 
4880613520 

您正在寻找的是一种在适当的位置进行分区的方法。据我所知,pandas不直接支持这一点,但有一种方法可以解决这一问题——将其分配给所有列,而不是数据帧

df1 = pd.DataFrame(np.arange(0, 50).reshape(10, 5))
df2 = pd.DataFrame(np.arange(50, 100).reshape(10, 5))
df3 = pd.DataFrame(np.arange(100, 150).reshape(10, 5))

df_list = [df1, df2, df3]

for df in df_list: 
    df[df.columns] = df[df.columns] / 10
如果您
打印(df1)
,您将获得:

     0    1    2    3    4
0  0.0  0.1  0.2  0.3  0.4
1  0.5  0.6  0.7  0.8  0.9
2  1.0  1.1  1.2  1.3  1.4
3  1.5  1.6  1.7  1.8  1.9
4  2.0  2.1  2.2  2.3  2.4
5  2.5  2.6  2.7  2.8  2.9
6  3.0  3.1  3.2  3.3  3.4
7  3.5  3.6  3.7  3.8  3.9
8  4.0  4.1  4.2  4.3  4.4
9  4.5  4.6  4.7  4.8  4.9
关于就地与非就地操作的注意事项 根据评论中的一个问题,我将尝试解释我所说的就地操作是什么意思。基本上,就地操作会更改对象本身。非在位操作将创建新对象。下面的代码说明了这一点:

df1 = pd.DataFrame(np.arange(0, 6).reshape(3, 2))
print(df1) => results in 
   0  1
0  0  1
1  2  3
2  4  5

print(id(df1))
4880614608 ==> this is the id (address) of the original df1

df1 = df1 / 10 ==> this produces a __new__ dataframe. 
print(id(df1))
==> 4880613520 # note that this id is different than the previous one. 


df1[df1.columns] = df1[df1.columns] * 100
print(df1) ==> this updates df1, and it now contains: 
      0     1
0   0.0  10.0
1  20.0  30.0
2  40.0  50.0

print(id(df1)) ==> The id / address of df _did_ _not_ change: 
4880613520 
你可以直言不讳

df1, df2, df3 = [frame/1000 for frame in dflist]
dflist
中的数据帧无法访问变量名,直接引用值。在列表理解后重新分配变量名会得到我们期望的结果。希望这个解释能有所帮助。

你可以说得很清楚

df1, df2, df3 = [frame/1000 for frame in dflist]
dflist
中的数据帧无法访问变量名,直接引用值。在列表理解后重新分配变量名会得到我们期望的结果。希望这个解释能有所帮助。

试试这个

for df in df_list:
    # Apply divide only on the numeric columns.
    columns = df.select_dtypes("number").columns
    df[columns] = df[columns] / 1000
试试这个

for df in df_list:
    # Apply divide only on the numeric columns.
    columns = df.select_dtypes("number").columns
    df[columns] = df[columns] / 1000

嗨,阿比纳夫,谢谢你的建议!正如你所说,这不会更新原始数据帧本身。嗨,阿比纳夫,谢谢你的建议!正如您所说,这不会更新原始数据帧本身。我担心这不会更新数据帧,但我会研究星号解包操作符-听起来可能是正确的轨道。@CarloPalazzi
dflist=[*map(lambda x:x/1000,dflist)]
恐怕这不会更新数据帧,但我将研究星号解包操作符-听起来它可能是正确的轨道。@CarloPalazzi
dflist=[*map(lambda x:x/1000,dflist)]
这很有效,谢谢!不确定您为什么说pandas在使用例如df=df/10更改数据帧时不支持适当的划分。同样奇怪的是,适当的划分可以在列上工作,但不能在整个数据帧上工作。还是我误解了什么?我添加了一个关于就地变更和不就地变更的解释。看看答案。这很有效,谢谢!不确定您为什么说pandas在使用例如df=df/10更改数据帧时不支持适当的划分。同样奇怪的是,适当的划分可以在列上工作,但不能在整个数据帧上工作。还是我误解了什么?我添加了一个关于就地变更和不就地变更的解释。查看答案。数据框中的所有列都是数字。数据框中的所有列都是数字。