1

so i am trying to split a column in a DataFrame (df1) using the .str.split() function. column name is hyphenated (lat-lon). When i run the code below, python reads only the "lat" and ignores the "-lon" thereby returning

AttributeError: 'DataFrame' object has no attribute 'lat'

df1[['lat','lon']] = df1.lat-lon.str.split(" ",expand=True,) df1

How do i get python to read the entire column name (lat-lon) and not just lat?

mozway
  • 194,879
  • 13
  • 39
  • 75

2 Answers2

3
df1[['lat','lon']] = df1["lat-lon"].str.split(" ",expand=True,)
Avi Thaker
  • 455
  • 3
  • 10
1

You can't use attributes with hyphens. The name must be a valid python name (letters+digits+underscore, not starting with a digit).

A good practice is actually to never use attributes for column names in pandas.

Use:

df1[['lat','lon']] = df1['lat-lon'].str.split(" ",expand=True,)
mozway
  • 194,879
  • 13
  • 39
  • 75