如何使用 python 在 table 中插入一行值(不是空值?
How to insert a row of value in a table with python (not Null Values?
我有一个 table 乘法线如下:
table1
col1
col2
col2
行 1
1
2
3
行 2
3
4
6
第 3 行
4
5
7
第 4 行
5
4
6
第5行
6
2
3
第 6 行
7
4
6
我想改成这样:
table1
col1
col2
col2
行 1
1
2
3
行 2
3
4
6
table1
col1
col2
col2
行 1
4
5
7
第 3 行
5
4
6
table3
col1
col2
col2
第 4 行
6
2
3
第5行
7
4
6
也就是插入一行(title)来分隔,因为它们属于不同的subtable。
我尝试使用 insert
函数来插入 title
的值
d.DataFrame(np.insert(df.values, 0, values=["col1", "col1", "col3"], axis=0))
但在DataFrame的特定列中,所有值的类型必须相同。
我还使用 xlwings
(insert
函数)和 openpyxl
(insert_rows
函数)插入一行
但似乎他们只能插入一个空白值,而不是特定值。
构建完这个table之后,我会用它来设置一些样式。
在excel中我只需要复制粘贴,有什么灵活的方法吗?
或者插入可能不是一个好方法,只是拆分和合并tables(带字幕并保持格式)?
补充:
[数据link][1]
[1]: https://cowtransfer.com/s/a160ccec698a48,you 需要输入密码 454008
你可以试试:
s=pd.DataFrame([df.columns]*int(len(df)/2),columns=df.columns)
s.index=pd.Series(s.index+1).cumsum()
df=pd.concat([df,s]).sort_index().iloc[:-1].reset_index(drop=True)
df
的输出:
table1 col1 col2 col2
0 row1 1 2 3
1 row2 3 4 6
2 table1 col1 col2 col2
3 row3 4 5 7
4 row4 5 4 6
5 table1 col1 col2 col2
6 row5 6 2 3
7 row6 7 4 6
更新:
你可以试试:
s=pd.DataFrame([df.columns]*int(len(df)/16),columns=df.columns)
s.index=[x+1 for x in range(16,16*(len(s)+1),16)]
df=pd.concat([df,s]).sort_index().reset_index(drop=True)
#If needed to remove last row then use:
#df=df.iloc[:-1]
我用于测试的示例数据框:
import numpy as np
df=pd.DataFrame()
df['table1']=[f'row {x}' for x in range(65)]
df['col1']=np.random.randint(1,10,(65,1))
df['col2']=np.random.randint(1,10,(65,1))
df['col3']=np.random.randint(1,10,(65,1))
df.columns=[*df.columns[:-1],'col2']
我有一个 table 乘法线如下:
table1 | col1 | col2 | col2 |
---|---|---|---|
行 1 | 1 | 2 | 3 |
行 2 | 3 | 4 | 6 |
第 3 行 | 4 | 5 | 7 |
第 4 行 | 5 | 4 | 6 |
第5行 | 6 | 2 | 3 |
第 6 行 | 7 | 4 | 6 |
我想改成这样:
table1 | col1 | col2 | col2 |
---|---|---|---|
行 1 | 1 | 2 | 3 |
行 2 | 3 | 4 | 6 |
table1 | col1 | col2 | col2 |
行 1 | 4 | 5 | 7 |
第 3 行 | 5 | 4 | 6 |
table3 | col1 | col2 | col2 |
第 4 行 | 6 | 2 | 3 |
第5行 | 7 | 4 | 6 |
也就是插入一行(title)来分隔,因为它们属于不同的subtable。
我尝试使用 insert
函数来插入 title
d.DataFrame(np.insert(df.values, 0, values=["col1", "col1", "col3"], axis=0))
但在DataFrame的特定列中,所有值的类型必须相同。
我还使用 xlwings
(insert
函数)和 openpyxl
(insert_rows
函数)插入一行
但似乎他们只能插入一个空白值,而不是特定值。
构建完这个table之后,我会用它来设置一些样式。
在excel中我只需要复制粘贴,有什么灵活的方法吗?
或者插入可能不是一个好方法,只是拆分和合并tables(带字幕并保持格式)?
补充: [数据link][1]
[1]: https://cowtransfer.com/s/a160ccec698a48,you 需要输入密码 454008
你可以试试:
s=pd.DataFrame([df.columns]*int(len(df)/2),columns=df.columns)
s.index=pd.Series(s.index+1).cumsum()
df=pd.concat([df,s]).sort_index().iloc[:-1].reset_index(drop=True)
df
的输出:
table1 col1 col2 col2
0 row1 1 2 3
1 row2 3 4 6
2 table1 col1 col2 col2
3 row3 4 5 7
4 row4 5 4 6
5 table1 col1 col2 col2
6 row5 6 2 3
7 row6 7 4 6
更新:
你可以试试:
s=pd.DataFrame([df.columns]*int(len(df)/16),columns=df.columns)
s.index=[x+1 for x in range(16,16*(len(s)+1),16)]
df=pd.concat([df,s]).sort_index().reset_index(drop=True)
#If needed to remove last row then use:
#df=df.iloc[:-1]
我用于测试的示例数据框:
import numpy as np
df=pd.DataFrame()
df['table1']=[f'row {x}' for x in range(65)]
df['col1']=np.random.randint(1,10,(65,1))
df['col2']=np.random.randint(1,10,(65,1))
df['col3']=np.random.randint(1,10,(65,1))
df.columns=[*df.columns[:-1],'col2']