有没有办法将标题行添加到CSV而不将CSV加载到python的内存中?我有一个18GB的CSV,我想添加一个标题,我看到的所有方法都需要将CSV加载到内存中,这显然是不可行的.
如果我的csv文件太大而无法使用大熊猫(在本例中为35gb)加载到内存中,那么我知道可以使用块大小对文件进行分块处理。
但是我想知道是否可以根据列中的值更改块大小。
我有一个ID列,然后每个ID都有几行包含信息,如下所示:
ID, Time, x, y
sasd, 10:12, 1, 3
sasd, 10:14, 1, 4
sasd, 10:32, 1, 2
cgfb, 10:02, 1, 6
cgfb, 10:13, 1, 3
aenr, 11:54, 2, 5
tory, 10:27, 1, 3
tory, 10:48, 3, 5
ect...
Run Code Online (Sandbox Code Playgroud)
我不想将ID分成不同的块。例如,将处理大小为4的块:
ID, Time, x, y
sasd, 10:12, 1, 3
sasd, 10:14, 1, 4
sasd, 10:32, 1, 2
cgfb, 10:02, 1, 6
cgfb, 10:13, 1, 3 <--this extra line is included in the 4 chunk
ID, Time, x, y
aenr, 11:54, …Run Code Online (Sandbox Code Playgroud) 我正在尝试将具有三列(日期,开始,结束)的pandas数据帧转换为频率矩阵.我的输入数据框如下所示:
Date, Start, End
2016-09-02 09:16:00 18 16
2016-09-02 16:14:10 16 1
2016-09-02 06:17:21 18 17
2016-09-02 05:51:07 23 17
2016-09-02 18:34:44 18 17
2016-09-02 05:44:44 20 4
2016-09-02 09:25:22 18 17
2016-09-02 22:27:44 18 17
2016-09-02 16:02:46 0 18
2016-09-02 15:35:07 17 17
2016-09-02 16:06:42 8 17
2016-09-02 14:47:04 16 23
2016-09-02 07:47:24 20 1
...
Run Code Online (Sandbox Code Playgroud)
"开始"和"结束"的值是介于0和之间的整数23.'日期'是日期时间.我想要创建的频率矩阵是24乘24 csv,其中行i和列j是输入中'End'= i和'Start'= j的次数.例如,以上数据将创建:
0, 1, 2, 3, 4, 5, 6, 7, 8, …Run Code Online (Sandbox Code Playgroud) 如何从熊猫日期时间中删除日期、小时和秒,以便我只剩下分钟?我有一个格式的日期表:
Date
2015-04-18 23:33:58
2015-04-19 14:32:08
2015-04-20 18:42:44
2015-04-20 21:41:19
Run Code Online (Sandbox Code Playgroud)
而且我要:
Date
33
32
42
41
Run Code Online (Sandbox Code Playgroud)
我尝试使用的代码是:
fiveMin['Date'] = fiveMin['Date'] - pd.Timedelta(fiveMin['Date'], unit='s')
Run Code Online (Sandbox Code Playgroud)
删除秒,但是我收到错误:
Value must be Timedelta, string, integer, float, timedelta or convertible
Run Code Online (Sandbox Code Playgroud) 我目前有一个脚本,可以在给定的 pandas 系列的情况下绘制相对频率的直方图。代码是:
def to_percent3(y, position):
s = str(100 * y)
if matplotlib.rcParams['text.usetex'] is True:
return s + r'$\%$'
else:
return s + '%'
df = pd.read_csv('mycsv.csv')
waypointfreq = df['Waypoint Frequency(Secs)']
cumfreq = df['Waypoint Frequency(Secs)']
perctile = np.percentile(waypointfreq, 95) # claculates 95th percentile
bins = np.arange(0,perctile+1,1) # creates list increasing by 1 to 96th percentile
plt.hist(waypointfreq, bins = bins, normed=True)
formatter = FuncFormatter(to_percent3) #changes y axis to percent
plt.gca().yaxis.set_major_formatter(formatter)
plt.axis([0, perctile, 0, 0.03]) #Defines the axis' by the 95th percentile …Run Code Online (Sandbox Code Playgroud) 我有一个包含列的 SQL 表:
ID, DayNumber, Mfm, value
432080971, 1, 15, 57
432080971, 1, 15, 59
432080978, 3, 15, 54
432080978, 4, 45, 54
Run Code Online (Sandbox Code Playgroud)
不幸的是,有一些重复的条目。我想要的是一个选择语句,它返回没有重复 ID、Daynumber 和 Mfm 的表,以及如果有双重条目来选择具有更高值的行。
因此,作为示例,上述条目将返回为:
ID, DayNumber, Mfm, value
432080971, 1, 15, 59
432080978, 3, 15, 54
432080978, 4, 45, 54
Run Code Online (Sandbox Code Playgroud)
我正在使用运行 sql server 2012 的 sql server management studio
python ×4
pandas ×3
python-3.x ×2
chunks ×1
csv ×1
datetime ×1
matplotlib ×1
matrix ×1
sql ×1
sql-server ×1