python pandas 使用 "title" 将逗号分隔值放入列中
python pandas give comma separated values into columns with "title"
我在同一列中有一些 comma-separated 数据,我希望将每个值分成不同的列。
0 13.4119837, 42.082885, 13.4119837, 42.082885
1 11.6285463, 42.4193742, 11.6285463, 42.4193742
2 -3.606772, 39.460299, -3.606772, 39.460299
3 -0.515639, 38.988847, -0.515639, 38.988847
4 -2.403309, 37.241792, -2.403309, 37.241792
我用下面的方法做了分离
data['column_name'].str.split(",", n = 3, expand = True)
我得到的输出是
0 1 2 3
0 13.4119837 42.082885 13.4119837 42.082885
1 11.6285463 42.4193742 11.6285463 42.4193742
2 -3.606772 39.460299 -3.606772 39.460299
3 -0.515639 38.988847 -0.515639 38.988847
4 -2.403309 37.241792 -2.403309 37.241792
但我需要类似下面的内容(必须为每一列提供一些标题)
minLat maxLat minLong maxLong
0 13.4119837 42.082885 13.4119837 42.082885
1 11.6285463 42.4193742 11.6285463 42.4193742
2 -3.606772 39.460299 -3.606772 39.460299
3 -0.515639 38.988847 -0.515639 38.988847
4 -2.403309 37.241792 -2.403309 37.241792
我该怎么做?
使用df.columns
:
data = data['column_name'].str.split(",", n = 3, expand = True)
data.columns = ['minLat', 'maxLat', 'minLong', 'maxLong']
我在同一列中有一些 comma-separated 数据,我希望将每个值分成不同的列。
0 13.4119837, 42.082885, 13.4119837, 42.082885
1 11.6285463, 42.4193742, 11.6285463, 42.4193742
2 -3.606772, 39.460299, -3.606772, 39.460299
3 -0.515639, 38.988847, -0.515639, 38.988847
4 -2.403309, 37.241792, -2.403309, 37.241792
我用下面的方法做了分离
data['column_name'].str.split(",", n = 3, expand = True)
我得到的输出是
0 1 2 3
0 13.4119837 42.082885 13.4119837 42.082885
1 11.6285463 42.4193742 11.6285463 42.4193742
2 -3.606772 39.460299 -3.606772 39.460299
3 -0.515639 38.988847 -0.515639 38.988847
4 -2.403309 37.241792 -2.403309 37.241792
但我需要类似下面的内容(必须为每一列提供一些标题)
minLat maxLat minLong maxLong
0 13.4119837 42.082885 13.4119837 42.082885
1 11.6285463 42.4193742 11.6285463 42.4193742
2 -3.606772 39.460299 -3.606772 39.460299
3 -0.515639 38.988847 -0.515639 38.988847
4 -2.403309 37.241792 -2.403309 37.241792
我该怎么做?
使用df.columns
:
data = data['column_name'].str.split(",", n = 3, expand = True)
data.columns = ['minLat', 'maxLat', 'minLong', 'maxLong']