-1

I am trying to replicate this function that is available in excel to delimitate one column into multiple columns that will then become integer fields. (See below the structure of the data of the one column).

precipitationDepthsInches
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.04, 1.02
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.04, 0.91
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 5.67, 5.94
Eric Shreve
  • 21
  • 1
  • 7
  • Does this answer your question? [Pandas split column of lists into multiple columns](https://stackoverflow.com/questions/35491274/pandas-split-column-of-lists-into-multiple-columns) – G. Anderson Aug 28 '20 at 22:26
  • I should mention that I have other columns in the dataframe that I need to preserve. Given this solution creates a new dataframe that would be a limitation for me. – Eric Shreve Aug 28 '20 at 22:33
  • 1
    The linked question shows both how to add it as columns to an existing dataframe and how to make a new dataframe which could be merged or concatted back to the original – G. Anderson Aug 28 '20 at 22:37
  • You can use `df = df.join(df.column_name.str.split(', '))`... but how are you getting that column? Perhaps you can split before creating it – RichieV Aug 28 '20 at 22:38

1 Answers1

0

If you are trying to split a comma separated text column into separate columns, here is what you can do:

# an example dataframe
df = pd.DataFrame({'csv_values':['1,2,3,4','5,6,7,8']})

# then split the text column like this:
split_cols = df['csv_values'].str.split(',',expand=True)

# then rename the columns if you like
split_cols.columns = [f'Split-{i}' for i in range(4)]

# then join back to the original dataFrame if you like:
new_df = df.join(split_cols)
fusion
  • 1,327
  • 6
  • 12