如何在循环期间向数据帧添加新的原始数据并满足特定条件?
how to add new raw to the data frame during a loop and a certain condition is met?
我想在迭代到达具有 'total charges' 的原始数据时添加一个新行。
仅供参考:如代码所示,第 1 列是必须执行的位置。
python
for row in df.itertuples():
row[1] == 'Total Charges'
this is how the data look like, i need to separate it with a row, right under total charges
使用:
import pandas as pd
s = list(range(3))
s.append('Total Charges')
s.extend(list(range(3)))
df = pd.DataFrame({'c1': s, 'c2': range(7)})
ind = df[df['c1']=='Total Charges'].index
df.loc[ind[0]+.5]='',''
df = df.sort_index().reset_index()
del df['index']
输出:
希望我理解正确(我使用了您提供的数据示例)。
迭代行并搜索 Total Charges
。然后使用 pandas.concat()
.
import pandas as pd
df = pd.DataFrame({'column1': ['data_row_1', 'data_row_2', 'Total Charges', 'data_row_3', 'data_row_4'], 'column2': range(1, 6)})
for index, row in df.iterrows():
if row['column1'] == 'Total Charges':
df_before = df.iloc[:index+1]
df_after = df.iloc[index+1:]
new_row = pd.DataFrame({'column1': ['new_data_1'], 'column2': ['new_data_2']})
new_df = pd.concat([df_before, new_row, df_after], ignore_index=True)
break
print(new_df)
输出:
column1 column2
0 data_row_1 1
1 data_row_2 2
2 Total Charges 3
3 new_data_1 new_data_2
4 data_row_3 4
5 data_row_4 5
我想在迭代到达具有 'total charges' 的原始数据时添加一个新行。 仅供参考:如代码所示,第 1 列是必须执行的位置。
python
for row in df.itertuples():
row[1] == 'Total Charges'
this is how the data look like, i need to separate it with a row, right under total charges
使用:
import pandas as pd
s = list(range(3))
s.append('Total Charges')
s.extend(list(range(3)))
df = pd.DataFrame({'c1': s, 'c2': range(7)})
ind = df[df['c1']=='Total Charges'].index
df.loc[ind[0]+.5]='',''
df = df.sort_index().reset_index()
del df['index']
输出:
希望我理解正确(我使用了您提供的数据示例)。
迭代行并搜索 Total Charges
。然后使用 pandas.concat()
.
import pandas as pd
df = pd.DataFrame({'column1': ['data_row_1', 'data_row_2', 'Total Charges', 'data_row_3', 'data_row_4'], 'column2': range(1, 6)})
for index, row in df.iterrows():
if row['column1'] == 'Total Charges':
df_before = df.iloc[:index+1]
df_after = df.iloc[index+1:]
new_row = pd.DataFrame({'column1': ['new_data_1'], 'column2': ['new_data_2']})
new_df = pd.concat([df_before, new_row, df_after], ignore_index=True)
break
print(new_df)
输出:
column1 column2
0 data_row_1 1
1 data_row_2 2
2 Total Charges 3
3 new_data_1 new_data_2
4 data_row_3 4
5 data_row_4 5