我有以下pandas数据帧:
Name Sheet Row Column Value
0 V1 Sheet1 1 1 89
1 V2 Sheet1 2 1 104
2 V3 Sheet2 1 2 6
3 V4 Sheet3 3 3 -5
我想将Value保存在数据框中指定的工作表,行和列中。它们不合适,我不知道会有多少张。我可以浏览每一行并使用xlwt包
进行编写ws = wb.add_sheet('Sheet1')
ws.write(1, 1, 89)
我想知道我是否可以使用pandas API或任何更好的方法实现这一目标?无法看到pandas提供对excel的特定行和列的访问以进行写入。
答案 0 :(得分:1)
其中一种方法是在for循环和<form class="form-horizontal" method="POST" action="{{ route('register') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="control-label">Name</label>
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="control-label">E-Mail Address</label>
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required>
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="control-label">Password</label>
<input id="password" type="password" class="form-control" name="password" required>
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
<div class="form-group">
<label for="password-confirm" class="control-label">Confirm Password</label>
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
<div class="form-group">
<div id="logonForgot">
<label for="promo-code" class="control-label">Promo Code*</label>
<input id="promo-code" type="text" class="form-control" name="promo_code" required>
</div>
<button id="btnSubmit" type="submit" class="btn btn-primary">
Register
</button>
</div>
即
iterrows()
希望它有所帮助。
答案 1 :(得分:1)
我找不到直接写入的API,但是,我发现xlwings
模块非常有用且直观。使用以下代码:
import xlwings as xw
wb = xw.Book('out.xls')
for index, row in result.iterrows():
wb.sheets[row['Sheet_Name']].range((row['Row'], row['Column'])).value = row['transformed_value']
wb.save()
答案 2 :(得分:0)
对相关的行/列选择使用切片,然后使用to_excel函数转储到Excel工作表中。
to_write_df = ws[(ws['Row']==1) & (ws['Column']==1)]
to_write_df.to_excel('output_file_path')