2016-05-16 29 views
0

我正试图从长数据集和大数据集中只清理一列。数据有18列,超过10k +行约100s的csv文件,其中我只想清理一列。从长数据集和大数据集中清理一列

输入字段只能从一长串几个

userLocation, userTimezone, Coordinates, 
India,   Hawaii, {u'type': u'Point', u'coordinates': [73.8567, 18.5203]} 
California,  USA  
      ,  New Delhi, 
Ft. Sam Houston,Mountain Time (US & Canada),{u'type': u'Point', u'coordinates': [86.99643, 23.68088]} 
Kathmandu,Nepal, Kathmandu, {u'type': u'Point', u'coordinates': [85.3248024, 27.69765658]} 

全部输入文件:Dropbox link

代码:

import pandas as pd 

    data = pandas.read_cvs('input.csv') 

    df = ['tweetID', 'tweetText', 'tweetRetweetCt', 'tweetFavoriteCt',  
      'tweetSource', 'tweetCreated', 'userID', 'userScreen', 
      'userName', 'userCreateDt', 'userDesc', 'userFollowerCt', 
      'userFriendsCt', 'userLocation', 'userTimezone', 'Coordinates', 
      'GeoEnabled', 'Language'] 

    df0 = ['Coordinates'] 

其他各列写入,因为它是在输出。这之后如何去做?

输出:

userLocation, userTimezone, Coordinate_one, Coordinate_one, 
India,   Hawaii,   73.8567, 18.5203 
California,  USA  
      ,  New Delhi, 
Ft. Sam Houston,Mountain Time (US & Canada),86.99643, 23.68088 
Kathmandu,Nepal, Kathmandu, 85.3248024, 27.69765658 

可能的最简单的建议,或直接我一些例子很多帮助。

回答

1

这里有很多错误。

  1. 该文件不是一个简单的csv文件,也没有被您假设的data = pd.read_csv('input.csv')正确解析。
  2. 提交的“坐标”似乎是一个json
  3. 有NaN的是在同一领域

这是我到目前为止已经完成。你会想自己做一些更合适的解析这个文件的工作

import pandas as pd 

df1 = pd.read_csv('./Turkey_28.csv') 

coords = df1[['tweetID', 'Coordinates']].set_index('tweetID')['Coordinates'] 

coords = coords.dropna().apply(lambda x: eval(x)) 
coords = coords[coords.apply(type) == dict] 

def get_coords(x): 
    return pd.Series(x['coordinates'], index=['Coordinate_one', 'Coordinate_two']) 

coords = coords.apply(get_coords) 

df2 = pd.concat([coords, df1.set_index('tweetID').reindex(coords.index)], axis=1) 

print df2.head(2).T 

tweetID           714602054988275712 
Coordinate_one            23.2745 
Coordinate_two            56.6165 
tweetText  I'm at MK Appartaments in Dobele https://t.co/... 
tweetRetweetCt             0 
tweetFavoriteCt             0 
tweetSource            Foursquare 
tweetCreated         2016-03-28 23:56:21 
userID             782541481 
userScreen           MartinsKnops 
userName            Martins Knops 
userCreateDt         2012-08-26 14:24:29 
userDesc   I See Them Try But They Can't Do What I Do. Be... 
userFollowerCt             137 
userFriendsCt             164 
userLocation          DOB Till I Die 
userTimezone           Casablanca 
Coordinates  {u'type': u'Point', u'coordinates': [23.274462... 
GeoEnabled             True 
Language              en 
+0

谢谢你的答案。 –

+0

非常感谢你..这已经解决了我的一半问题。 –

1

10K行看起来并不像大数据。你有多少列?

我不明白你的代码,它被打破,但一个简单的例子操作:

df = pd.read_cvs('input.csv') 
df['tweetID'] = df['tweetID'] + 1 # add 1 
df.to_csv('output.csv', index=False) 

如果你的数据不适合到内存中,你可能会考虑使用DASK。

+0

谢谢你的答案。 10K只是其中一个文件。我有超过100个这样的文件。原始文件中有18列。我只想清除输入文件中所有列的'coordinates'列和输出文件。 –

+1

你可以迭代地这样做。建立所有文件的列表,并在for循环中逐个处理它们。 – dukebody

+0

截至目前我想正确清理一个文件..为此我面临太多挫折:( –