您好我有一个
形式的csv文件Col1 Col2 Col3 Col4 col5
AA BA CA DA EA
AB BB CB DB EB
AC BC CC DC EC
AD BD CD DD ED
如何使用新列COLTAble创建新的csv文件 新的csv将采用这种格式。 Col1 Col2 Coltable coltable使用col3,col4和col5,并采用
的形式<table border="1" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td rowspan="2" style="width:50%;">
COL3
</td>
<td style="width:50%;">
COL4
</td>
</tr>
<tr>
<td style="width:50%;">
COL5
</td>
</tr>
</tbody>
答案 0 :(得分:1)
我不确定我完全理解你的问题。你的最终csv每行都有不同的html表字符串吗?
尽管如此,pandas对于读取和写入这样的文件非常有用。
>>> import pandas as pd
>>> df = pd.read_csv('temp.csv')
>>> print df
Col1 Col2 Col3 Col4 col5
0 AA BA CA DA EA
1 AB BB CB DB EB
2 AC BC CC DC EC
3 AD BD CD DD ED
>>> df['Coltable'] = df.apply(lambda row: row[2:].to_frame().to_html(), axis=1)
>>> df_new = df.iloc[:, [0, 1, -1]]
>>> print df_new
Col1 Col2 Coltable
0 AA BA <table border="1" class="dataframe">\n <thead...
1 AB BB <table border="1" class="dataframe">\n <thead...
2 AC BC <table border="1" class="dataframe">\n <thead...
3 AD BD <table border="1" class="dataframe">\n <thead...
df_new.to_csv('temp_new.csv', index=False)
>>> df_new.to_csv('temp_new.csv', index=False)
现在temp_new.csv看起来像
Col1,Col2,Coltable
AA,BA,"<table border=""1"" class=""dataframe"">
<thead>
<tr style=""text-align: right;"">
<th></th>
<th>0</th>
</tr>
</thead>
<tbody>
<tr>
<th>Col3</th>
<td>CA</td>
</tr>
<tr>
<th>Col4</th>
<td>DA</td>
</tr>
<tr>
<th>col5</th>
<td>EA</td>
</tr>
</tbody>
</table>"
AB,BB,"<table border=""1"" class=""dataframe"">
<thead>
<tr style=""text-align: right;"">
<th></th>
<th>1</th>
</tr>
</thead>
<tbody>
<tr>
<th>Col3</th>
<td>CB</td>
</tr>
<tr>
<th>Col4</th>
<td>DB</td>
</tr>
<tr>
<th>col5</th>
<td>EB</td>
</tr>
</tbody>
</table>"
AC,BC,"<table border=""1"" class=""dataframe"">
<thead>
<tr style=""text-align: right;"">
<th></th>
<th>2</th>
</tr>
</thead>
<tbody>
<tr>
<th>Col3</th>
<td>CC</td>
</tr>
<tr>
<th>Col4</th>
<td>DC</td>
</tr>
<tr>
<th>col5</th>
<td>EC</td>
</tr>
</tbody>
</table>"
AD,BD,"<table border=""1"" class=""dataframe"">
<thead>
<tr style=""text-align: right;"">
<th></th>
<th>3</th>
</tr>
</thead>
<tbody>
<tr>
<th>Col3</th>
<td>CD</td>
</tr>
<tr>
<th>Col4</th>
<td>DD</td>
</tr>
<tr>
<th>col5</th>
<td>ED</td>
</tr>
</tbody>
</table>"