Python 从长数据和大数据集中清除一列
我试图从长而大的数据集中只清理一列。数据有18列,超过10k+行,大约100个csv文件,其中我只想清理一列 仅输入长列表中的几个字段Python 从长数据和大数据集中清除一列,python,pandas,data-cleaning,bigdata,Python,Pandas,Data Cleaning,Bigdata,我试图从长而大的数据集中只清理一列。数据有18列,超过10k+行,大约100个csv文件,其中我只想清理一列 仅输入长列表中的几个字段 userLocation, userTimezone, Coordinates, India, Hawaii, {u'type': u'Point', u'coordinates': [73.8567, 18.5203]} California, USA , New Delhi,
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]}
完整输入文件:
代码:
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
可能最简单的建议或指导我一些例子将是非常有帮助的 10K行看起来根本不像大数据。你有多少列 我不理解您的代码,它已损坏,但有一个简单的操作示例:
df = pd.read_cvs('input.csv')
df['tweetID'] = df['tweetID'] + 1 # add 1
df.to_csv('output.csv', index=False)
如果你的数据不适合内存,你可以考虑使用DASK。
10K行看起来不象大数据。你有多少列
我不理解您的代码,它已损坏,但有一个简单的操作示例:df = pd.read_cvs('input.csv')
df['tweetID'] = df['tweetID'] + 1 # add 1
df.to_csv('output.csv', index=False)
如果你的数据不适合内存,你可以考虑使用DASK。
这里有很多错误。
data=pd.read\u csv('input.csv')
正确解析json
字符串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
这里有很多错误
data=pd.read\u csv('input.csv')
正确解析json
字符串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
谢谢你的回答。10K仅用于其中一个文件。我喜欢100多个这样的文件。原始文件中有18列。我只想清理
坐标列和输出文件中的所有输入列,您可以迭代地这样做。建立一个所有文件的列表,并在for循环中逐个处理它们。到现在为止,我想正确地清理一个文件。。为此,我面临着太多的挫折:(谢谢你的回答。10K只是其中一个文件。我喜欢100多个这样的文件。原始文件中有18列。我只想清理坐标列和输出文件中的所有输入列。你可以迭代地这样做。建立一个所有文件的列表,并在for循环中逐个处理它们。到目前为止,我(谢谢你的回答。非常感谢。这已经解决了我一半的问题。谢谢你的回答。非常感谢。这已经解决了我一半的问题。