如何遍历多列数据框中的每个单独的列值?

时间:2018-10-21 07:51:53

标签: python pandas dataframe

我有多列数据框,其中包含列 [“国家”,“能源供应”,“人均能源供应”,“可再生百分比”]

在能源供应列中,我想将列的单位从Giga转换为Peta。但是在过程中 energy['Energy Supply']*= energy['Energy Supply']的值类似于“ ....”(缺失值由此表示)时,也会相乘或重复。同样,该列中的字符串值也将相乘。 (例如,对于原始文件:Peta,操作后:PetaPetaPetaPeta ...)。

为阻止这种情况的发生,我正在运行此程序:

energy = pd.read_excel("Energy Indicators.xls",skiprows = 16, skip_footer = 38)
energy.drop(['Unnamed: 0','Unnamed: 1'],axis = 1, inplace = True)
energy.columns = ['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']
for i in energy['Energy Supply']:
    if (isinstance(energy[i],int) == True):
        energy['Energy Supply'][i]=energy['Energy Supply'][i]*1000000
return (energy)

但是我没有得到结果,即仅更改整数类型变量的值,并且没有任何变化。

我认为问题出在哪里,前两行将给出 false 条件,因为前几行是“字符串”,并且在此基础上,程序未修改值,而我希望分别检查该值是否为整数类型,如果是,则将该数字乘以1,000,000。

输入:

    Country        Energy Supply    Energy Supply per Capita    % Renewable
0   NaN             Petajoules            Gigajoules                 %
1   Afghanistan        321                   10                  78.6693
2   Albania            102                   35                    100
3   Algeria            1959                  51                  0.55101
4   American Samoa      ...                 ...                  0.641026

预期输出:

    Country        Energy Supply    Energy Supply per Capita    % Renewable
0   NaN             Petajoules            Gigajoules                 %
1   Afghanistan        3210000                10                     78.6693
2   Albania            1020000                35                      100
3   Algeria            19590000               51                     0.55101
4   American Samoa      ...                 ...                    0.641026

当前输出:

    Country        Energy Supply    Energy Supply per Capita    % Renewable
0   NaN             PetajoulesPeta.         Gigajoules               %
1   Afghanistan        3210000                10                   78.6693
2   Albania            1020000                35                    100
3   Algeria            19590000               51                   0.55101
4   American Samoa      ........                ...                0.641026

2 个答案:

答案 0 :(得分:1)

这对我来说具有一百万个值:

import pandas as pd
import numpy as np 

data = {"Energy Supply":[1,30,"Petajoules",5,70]*2000000}

energy = pd.DataFrame(data)

输入:

Energy Supply
0                   1
1                  30
2          Petajoules
3                   5
4                  70
5                   1
6                  30
7          Petajoules
8                   5
9                  70
10                  1
11                 30
12         Petajoules
13                  5
14                 70
15                  1
16                 30
17         Petajoules
18                  5
19                 70
20                  1
21                 30
22         Petajoules
23                  5
24                 70
25                  1
26                 30
27         Petajoules
28                  5
29                 70
              ...
[10000000 rows x 1 columns]

然后我将Series转换为数组并设置值:

arr = energy["Energy Supply"].values

for i in range(len(arr)):
    if isinstance(arr[i],int):
        arr[i] = arr[i]*1000000
    else:
        pass

输出看起来像这样:

        Energy Supply
0             1000000
1            30000000
2          Petajoules
3             5000000
4            70000000
5             1000000
6            30000000
7          Petajoules
8             5000000
9            70000000
10            1000000
11           30000000
12         Petajoules
13            5000000
14           70000000
15            1000000
16           30000000
17         Petajoules
18            5000000
19           70000000
20            1000000
21           30000000
22         Petajoules
23            5000000
24           70000000
25            1000000
26           30000000
27         Petajoules
28            5000000
29           70000000
              ...
[10000000 rows x 1 columns]

此解决方案的速度约为申请的两倍:

遍历数组:

loop: 100%|██████████| 10000000/10000000 [00:07<00:00, 1376439.75it/s]

使用“申请”:

apply: 100%|██████████| 10000000/10000000 [00:14<00:00, 687420.00it/s]

如果将系列转换为数字,则字符串值将变为nan值。使用np.where时,将序列转换为数值并乘以值都需要大约5秒钟:

import pandas as pd
import numpy as np 
import time

data = {"Energy Supply":[1,30,"Petajoules",5,70]*2000000}

energy = pd.DataFrame(data)
t = time.time()

energy["Energy Supply"] = pd.to_numeric(energy["Energy Supply"],errors="coerce")

energy["Energy_Supply"] = np.where((energy["Energy Supply"]%1==0),energy["Energy Supply"]*100,energy["Energy Supply"])
t1 = time.time()
print(t1-t)
5.275099515914917

但是您也可以在使用pd.to_numeric()之后简单地执行此操作:

energy["Energy Supply"] = energy["Energy Supply"]*1000000

答案 1 :(得分:1)

您可以使用str.isnumeric检查字符串是否为数字,然后相乘。

energy['Energy Supply'] = energy['Energy Supply'].apply(lambda x: int(x) * 1000000 if str(x).isnumeric() else x)

print (energy)

    Country         Energy Supply   Energy Supply per Capita    % Renewable
0             NaN    Petajoules           Gigajoules                     %
1     Afghanistan    321000000                10                   78.6693
2         Albania    102000000                35                       100
3         Algeria    1959000000               51                   0.55101 
4  American Samoa        ...                  ..                  0.641026