简体   繁体   English

Python pandas.read_csv使用逗号将列拆分为多个新列

[英]Python pandas.read_csv split column into multiple new columns using comma to separate

I've used pandas.read_csv to load in a file. 我使用pandas.read_csv加载文件。

I've stored the file into a variable. 我已将文件存储到变量中。 The first column is a series of numbers separated by a comma (,) I want to split these numbers, and put each number to a new column. 第一列是一系列数字,中间用逗号(,)分隔,我想将这些数字分割,然后将每个数字放到新列中。

I can't seem to find the write functionality for pandas.dataframe. 我似乎找不到pandas.dataframe的写入功能。

Side Note I would prefer a different library for loading in my file, but pandas provides some other different functionality which I need. 旁注我希望使用其他库来加载文件,但是pandas提供了我需要的其他一些功能。

My Code: 我的代码:

Data = pandas.read_csv(pathFile,header=None)

doing: print Data gives me: 这样做: print Data给我:

   0                          1         2          ...
0 [2014, 8, 26, 5, 30, 0.0]   0         0.25       ...

(as you can see its a date) (如您所见,它是一个日期)

Question: How to split/separate each number and save it in a new array 问题:如何分割/分隔每个数字并将其保存在新数组中

ps I'm trying to achieve the same thing the matlab method datevec() does ps我正在尝试实现matlab方法datevec()所做的相同操作

If the CSV data looks like 如果CSV数据看起来像

"[2014, 8, 26, 5, 30, 0.0]",0,0.25    

then 然后

import pandas as pd
import json

df = pd.read_csv('data', header=None)
dates, df = df[0], df.iloc[:, 1:]
df = pd.concat([df, dates.apply(lambda x: pd.Series(json.loads(x)))], axis=1,
               ignore_index=True)
print(df)

yields 产量

   0     1     2  3   4  5   6  7
0  0  0.25  2014  8  26  5  30  0

with the values parsed as numeric values. 将值解析为数字值。


How it works: 这个怎么运作:

dates, df = df[0], df.iloc[:, 1:]

peels off the first column, and reassigns df to the rest of the DataFrame: 剥离第一列,然后将df重新分配给DataFrame的其余部分:

In [217]: dates
Out[217]: 
0    [2014, 8, 26, 5, 30, 0.0]
Name: 0, dtype: object

dates contains strings: dates包含字符串:

In [218]: dates.iloc[0]
Out[218]: '[2014, 8, 26, 5, 30, 0.0]'

We can convert these to a list using json.loads : 我们可以使用json.loads将它们转换为列表:

In [219]: import json

In [220]: json.loads(dates.iloc[0])
Out[220]: [2014, 8, 26, 5, 30, 0.0]

In [221]: type(json.loads(dates.iloc[0]))
Out[221]: list

We can do this for each row of dates by using apply : 我们可以使用apply对每一行dates执行此操作:

In [222]: dates.apply(lambda x: pd.Series(json.loads(x)))
Out[222]: 
      0  1   2  3   4  5
0  2014  8  26  5  30  0

By making lambda , above, return a Series, apply will return a DataFrame, with the index of the Series becoming the column index of the DataFrame. 通过在上面创建lambda ,返回一个Series, apply将返回一个DataFrame,Series的索引成为该DataFrame的列索引。

Now we can use pd.concat to concatenate this DataFrame with df : 现在我们可以使用pd.concat将这个DataFrame与df连接起来:

In [228]: df = pd.concat([df, dates.apply(lambda x: pd.Series(json.loads(x)))], axis=1, ignore_index=True)

In [229]: df
Out[229]: 
   0     1     2  3   4  5   6  7
0  0  0.25  2014  8  26  5  30  0

In [230]: df.dtypes
Out[230]: 
0      int64
1    float64
2    float64
3    float64
4    float64
5    float64
6    float64
7    float64
dtype: object

How about 怎么样

df
#                   datestr
#0  2014, 8, 26, 5, 30, 0.0
#1  2014, 8, 26, 5, 30, 0.0
#2  2014, 8, 26, 5, 30, 0.0
#3  2014, 8, 26, 5, 30, 0.0
#4  2014, 8, 26, 5, 30, 0.0

# each entry is a string
df.datestr[0]
#'2014, 8, 26, 5, 30, 0.0'

Then 然后

date_order = ('year', 'month','day','hour','minute','sec') # order matters here, should match the datestr column 

for i,col in enumerate( date_order):
    df[col] = df.datestr.map( lambda x: x.split(',')[i].strip() )

#df
#                   datestr  year month day hour minute  sec
#0  2014, 8, 26, 5, 30, 0.0  2014     8  26    5     30  0.0
#1  2014, 8, 26, 5, 30, 0.0  2014     8  26    5     30  0.0
#2  2014, 8, 26, 5, 30, 0.0  2014     8  26    5     30  0.0
#3  2014, 8, 26, 5, 30, 0.0  2014     8  26    5     30  0.0
#4  2014, 8, 26, 5, 30, 0.0  2014     8  26    5     30  0.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM