我正在使用 python 中的 csv 檔案并試圖找出...
- 如何從檔案中洗掉 10 行(頂部或底部,提示用戶選擇)
- 如何將 10 行附加到 csv 檔案的頂部
- 如何在這些新創建的行中輸入資訊
任何資訊都有幫助!謝謝

uj5u.com熱心網友回復:
嘗試:
cols = your_columns #type list
new_row_as_df = pd.DataFrame([value_col1, value_col2, ..., val_col9], columns=your_columns)
new_row_as_list = [value_col1, value_col2, ..., val_col9]
# Add a new row to the top as df:
df = pd.concat([new_row_as_df, df]).reset_index(drop=True)
# Add a new row to the bottom as list:
df.loc[len(df.index)] = new_row_as_list
# Add a new row to the bottom as df:
df = pd.concat([df, new_row_as_df]).reset_index(drop=True)
# Delete a row at a specific index (int):
df.drop(labels=your_index, axis=0, inplace=True)
uj5u.com熱心網友回復:
洗掉特定行的簡單方法:
# delete a single row by index value 0
data = data.drop(labels=0, axis=0)
# delete a few specified rows at index values 0, 15, 20.
# Note that the index values do not always align to row numbers.
data = data.drop(labels=[1,15,20], axis=0)
# delete a range of rows - index values 10-20
data = data.drop(labels=range(40, 45), axis=0)
@Sergey 剛剛回答了如何將行添加到底部或頂部。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/478041.html
