# 插入空白行熊猫数据框 - python

``````for i in range(0, end):
if df2.at[i + 1, 'factor'] != df2.at[i, 'factor']:
``````

`for`循环中顺序手动插入行效率低下。或者，您可以找到发生更改的索引，构造一个新的数据框，连接，然后按索引排序：

``````df = pd.DataFrame([[1, 1], [2, 1], [3, 2], [4, 2],
[5, 2], [6, 3]], columns=['A', 'B'])

switches = df['B'].ne(df['B'].shift(-1))
idx = switches[switches].index

df_new = pd.DataFrame(index=idx + 0.5)
df = pd.concat([df, df_new]).sort_index()

print(df)

A    B
0.0  1.0  1.0
1.0  2.0  1.0
1.5  NaN  NaN
2.0  3.0  2.0
3.0  4.0  2.0
4.0  5.0  2.0
4.5  NaN  NaN
5.0  6.0  3.0
5.5  NaN  NaN
``````

``````print(df.reset_index(drop=True))

A    B
0  1.0  1.0
1  2.0  1.0
2  NaN  NaN
3  3.0  2.0
4  4.0  2.0
5  5.0  2.0
6  NaN  NaN
7  6.0  3.0
8  NaN  NaN
``````

Python exchangelib在子文件夹中读取邮件 - python

Python:如何根据另一列元素明智地查找一列中的空单元格计数？ - python

df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice','Jane', 'Alice','Bob', 'Alice'], 'income…

pandas DataFrame:根据另一列中的布尔值计算总和 - python

Python GPU资源利用 - python
{"ft_lang_label":"__label__zh","ft_lang_prob":0.7023512,"math_prob":0.98662883,"size":1691,"snap":"2023-14-2023-23","text_gpt3_token_len":1040,"char_repetition_ratio":0.10729105,"word_repetition_ratio":0.0,"special_character_ratio":0.3151981,"punctuation_ratio":0.2534819,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9922481,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-05-30T10:43:22Z\",\"WARC-Record-ID\":\"<urn:uuid:d906a9f4-f1cf-4fc3-9087-8ffd42875b22>\",\"Content-Length\":\"29919\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d33f1e3e-b957-4fca-8ab2-2d1012f261f1>\",\"WARC-Concurrent-To\":\"<urn:uuid:1045ed1a-615f-4d89-b257-5c20f8278377>\",\"WARC-IP-Address\":\"172.67.148.94\",\"WARC-Target-URI\":\"https://www.45ma.com/post-76891.html\",\"WARC-Payload-Digest\":\"sha1:PESEZB35GA3CWK7OCMXWOD6N75DRZ5O2\",\"WARC-Block-Digest\":\"sha1:X344OVUYKDWKZG5DLBGXL56HUFSAS5XR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224645595.10_warc_CC-MAIN-20230530095645-20230530125645-00405.warc.gz\"}"}