Python 如何使用从现有列提取的数据创建新的DataFrame列

Python 如何使用从现有列提取的数据创建新的DataFrame列,python,dataframe,Python,Dataframe,大家好,我有以下数据框: Index Numbering Description 1 A Agri. and Forest 2 1 Agri. 3 1.1 ----------- 4 1.2 ----------- 5 1.3

大家好,我有以下数据框:

  Index    Numbering           Description
    1          A            Agri. and Forest
    2          1                  Agri.
    3         1.1              -----------
    4         1.2              -----------
    5         1.3              -----------
    6          2                  Forest
    7         2.1              -----------
    8         2.3              -----------
    9         2.4              -----------
   10          B               Manufacturing
   11          3                  Autos
   12         3.1              -----------
   13         3.2              -----------
   14         3.3              -----------
我想用从现有列中提取的值创建两个新列。我希望实现以下目标:

   Index     Numbering       Description         Letter     Number
    1           A           Agri. and Forest        A       
    2           1                 Agri.             A         1
    3          1.1             -----------          A         1
    4          1.2             -----------          A         1
    5          1.3             -----------          A         1
    6           2                 Forest            A         2
    7          2.1             -----------          A         2
    8          2.3             -----------          A         2
    9          2.4             -----------          A         2
   10           B              Manufacturing        B
   11           3                 Autos             B         3
   12          3.1             -----------          B         3
   13          3.2             -----------          B         3
   14          3.3             -----------          B         3

非常感谢您的想法。

我用这种方法解决了这个问题。(假设您可以在excel中以CSV格式保存数据)

结果 另一种方式 我不知道为什么前一个不适合你。但这是一个可能奏效的微小变化。看看吧

import numpy as np
import pandas as pd
import math
letter=''
data1 = pd.read_csv('C:/random/d1', sep=',', header=None,names=['C1','C2'])

df1=pd.DataFrame(data1)
dfNew=pd.DataFrame(columns=['C1','C2','C3','C4'])


(rows,columns)=df1.shape

for index in range(rows):
    try:
        c1=float(df1.iat[index,0])
    except:
        c1=df1.iat[index,0]

    if(isinstance(c1,float) ):
        number=math.floor(c1)
    else:
        letter=df1.iat[index,0]
        number=''

    tempRow=[df1.iat[index,0],df1.iat[index,1],letter,number]
    dfNew.loc[len(dfNew)]=tempRow

print()
print(dfNew)
结果(相同)
添加A、B、1、2或3的逻辑是什么?是基于其他列还是基于单元格?原始数据来自excel单元格;所需列数据应来自现有列“编号”。在新列中,“字母”A应包含在每行中,直到在“编号”列中满足B。然后开始B。关于“编号”中的数字,无论是1或1.2或1.3,在新列中应仅显示1,即第一位数字。感谢Rebin,我认为该代码可以工作,但我正在努力使用我导入的原始数据中“C1”列中的数据格式。当我运行代码时,我得到以下错误:“'int'对象没有属性'isalpha'”。它显示在if语句的开头,其中“isalpha”是。我检查了“C1”的数据类型,它显示为dtype('O')。我将其转换为string,但当我再次运行代码时,我得到以下结果:无法将string转换为float:'-这显示在else参数后的行中。有什么建议我的数据类型应该在“C1”列吗?
     C1                C2 C3 C4
0     A  Agri. and Forest  A
1     1             Agri.  A  1
2   1.1       -----------  A  1
3   1.2       -----------  A  1
4   1.3       -----------  A  1
5     2            Forest  A  2
6   2.1       -----------  A  2
7   2.3       -----------  A  2
8   2.4       -----------  A  2
9     B     Manufacturing  B
10    3             Autos  B  3
11  3.1       -----------  B  3
12  3.2       -----------  B  3
13  3.3       -----------  B  3
import numpy as np
import pandas as pd
import math
letter=''
data1 = pd.read_csv('C:/random/d1', sep=',', header=None,names=['C1','C2'])

df1=pd.DataFrame(data1)
dfNew=pd.DataFrame(columns=['C1','C2','C3','C4'])


(rows,columns)=df1.shape

for index in range(rows):
    try:
        c1=float(df1.iat[index,0])
    except:
        c1=df1.iat[index,0]

    if(isinstance(c1,float) ):
        number=math.floor(c1)
    else:
        letter=df1.iat[index,0]
        number=''

    tempRow=[df1.iat[index,0],df1.iat[index,1],letter,number]
    dfNew.loc[len(dfNew)]=tempRow

print()
print(dfNew)
     C1                C2 C3 C4
0     A  Agri. and Forest  A
1     1             Agri.  A  1
2   1.1       -----------  A  1
3   1.2       -----------  A  1
4   1.3       -----------  A  1
5     2            Forest  A  2
6   2.1       -----------  A  2
7   2.3       -----------  A  2
8   2.4       -----------  A  2
9     B     Manufacturing  B
10    3             Autos  B  3
11  3.1       -----------  B  3
12  3.2       -----------  B  3
13  3.3       -----------  B  3
Press any key to continue . . .