[英]split multiple columns by separator pandas
How to split every column by ":", using pandas如何使用 Pandas 按“:”分割每一列
,0,1,2,3,4
0,MMSI : 222111345,Country : Singa,Ship name : xxxVessel,Call sign : abcd,IMO number : 12345
1,MMSI : 222111345,Country : Singa,Ship name : xxxVessel,Call sign : abcd,IMO number : 12345
2,MMSI : 222111345,Country : Singa,Ship name : xxxVessel,Call sign : abcd,IMO number : 12345
3,MMSI : 222111345,Country : Singa,Ship name : xxxVessel,Call sign : abcd,IMO number : 12345
I tryed df3 = df3[df3.columns[0]].str.split(":",expand=True)
but doesn´t work我
df3 = df3[df3.columns[0]].str.split(":",expand=True)
但不起作用
You can try via concat()
and list comprehension:您可以尝试通过
concat()
和列表理解:
df=pd.concat([df3[x].str.split(':',expand=True) for x in df3],axis=1)
Note: If there are integer and float values in columns then:注意:如果列中有整数和浮点值,则:
cols=df3.columns[df3.dtypes=='O']
#Filtered out columns that are of type Object
df=pd.concat([df3[x].str.split(':',expand=True) for x in cols],axis=1)
Note:笔记:
If you are reading csv file then you can also try this :如果您正在阅读 csv 文件,那么您也可以尝试以下操作:
df3=pd.read_csv("filename.csv", sep=",|:").reset_index()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.