Python 将数据框列拆分为新的4列
我有这个熊猫df,我想把地址列(最后一个)分为4个新列streename+num、zipcode、City和land 试验 我想得到这个输出Python 将数据框列拆分为新的4列,python,pandas,Python,Pandas,我有这个熊猫df,我想把地址列(最后一个)分为4个新列streename+num、zipcode、City和land 试验 我想得到这个输出 ID Street zipcode city country 1.10065e+08 Bachgasse39 69502 Hemsbach Deutschland 2.34115e+08 Am Friedensplatz3 68165 Mannheim Deutschland 2.36743
ID Street zipcode city country
1.10065e+08 Bachgasse39 69502 Hemsbach Deutschland
2.34115e+08 Am Friedensplatz3 68165 Mannheim Deutschland
2.36743e+08 Am Friedensplatz3 68165 Mannheim Deutschland
2.24763e+08 Am Friedensplatz3 68165 Mannheim Deutschland
2.26209e+08 Am Friedensplatz3 68165 Mannheim Nan
2.2621e+08 Am Friedensplatz3 68165 Mannheim Nan
.... ....... ..... .... ....
.... ...... ...... .... ......
我尝试过这种方法来解决这个问题,但不适用于我:
(A、B、C、D)是(街道名称+num、Zipcode…)的列名
但我有一个错误:
TypeError:类型为“float”的对象没有len()
这里还有提示:
我想这样:
我的数据框中有以下地址模式:
鉴于你的专栏
坚定的地址
Geschäftlich
是字符串,您可以尝试以下操作:
df1=pd.DataFrame(test['Firmen Adresse
Geschäftlich'].str.split(r"\n").tolist(),columns = ['street
no.','zip','Land'],index=test['ID'])
df1[['zip','Stadt']]=pd.DataFrame(df1['zip'].str.strip().str.split('
').tolist(),index = df1.index)
日期集较小的输出如下所示:
street no. zip Land Stadt
ID
1 Bachgasse 39 69502 Deutschland Hemsbach
2 Am Friedensplatz 3 68165 Deutschland Mannheim
您的列名末尾缺少一个引号。您可以只使用expand参数:
df['col'].str.split('\n',expand=True)
而不是发布图像,您可以在文本中发布数据帧和代码吗?这对我们和你都有帮助@Yo_Chris我得到了TypeError:“float”类型的对象没有len()@tianlinhe刚刚更新了我的question@Adam确保正在执行str
方法的列确实是字符串
df1=pd.DataFrame(test['Firmen Adresse
Geschäftlich'].str.split(r"\n").tolist(),columns = ['street
no.','zip','Land'],index=test['ID'])
df1[['zip','Stadt']]=pd.DataFrame(df1['zip'].str.strip().str.split('
').tolist(),index = df1.index)
street no. zip Land Stadt
ID
1 Bachgasse 39 69502 Deutschland Hemsbach
2 Am Friedensplatz 3 68165 Deutschland Mannheim