[英]Split pandas dataframe column to new 4 columns
I have this Pandas df and I would to spilt the Adress column (Last one) to 4 new columns Stree name + num, zipcode, City and land.我有这个 Pandas df,我会将 Adress 列(最后一个)溢出到 4 个新列 Stree name + num、zipcode、City 和 land。
test测试
ID Address
1.10065e+08 Bachgasse 39 \n69502 Hemsbach \nDeutschland
2.34115e+08 Am Friedensplatz 3\n68165 Mannheim\nDeutschland
2.36743e+08 Am Friedensplatz 3\n68165 Mannheim\nDeutschland
2.24763e+08 Am Friedensplatz 3\n68165 Mannheim\nDeutschland
2.26209e+08 Am Friedensplatz 3\n68165 Mannheim
2.2621e+08 Am Friedensplatz 3\n68165 Mannheim
2.35501e+08 Herman-BurcharStrasse 1\n7265 Davos Wolfgang\n...
2.31895e+08 Via Nova 37\n7017 Flims Dorf\nSchweiz
2.3611e+08 Neu-Isenburg\nDeutschland
2.40194e+08 Herman-BurcharStrasse 1\n7265 Davos Wolfgang\n.
I would like to get this output我想得到这个 output
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
.... ....... ..... .... ....
.... ...... ...... .... ......
I've tried this approache to solve that but doesn't work for me:我已经尝试过这种方法来解决这个问题,但对我不起作用:
(A,B,C,D) are column names for (Street name + num, Zipcode...) (A,B,C,D) 是 (Street name + num, Zipcode...) 的列名
pd.DataFrame(test['Firmen Adresse Geschäftlich'].str.split(r"\n",1).tolist(),columns = ['A','B','C'])
but i got this error:但我收到了这个错误:
TypeError: object of type 'float' has no len() TypeError: 'float' 类型的 object 没有 len()
Here also imges:这里还有图片:
I would like to get like this:我想得到这样的:
I have these addresses patterns in my dataframe:我的 dataframe 中有这些地址模式:
Given that your column Firmen Adresse Geschäftlich
is string, you can try the following:鉴于您的列Firmen Adresse 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)
The output with a smaller dateset looks like:具有较小日期集的 output 如下所示:
street no. zip Land Stadt
ID
1 Bachgasse 39 69502 Deutschland Hemsbach
2 Am Friedensplatz 3 68165 Deutschland Mannheim
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.