Pandas数据框:通过多个逗号分隔符将文本转换为列

时间:2020-08-28 22:23:35

标签: python pandas dataframe

我正在尝试复制此功能,该功能可以在excel中使用,以将一列分隔为多列,然后这些列将成为整数字段。 (请参阅下面的一列数据结构。)

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

1 个答案:

答案 0 :(得分:0)

如果您尝试将逗号分隔的文本列拆分为单独的列,则可以执行以下操作:

# 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)