I have a DataFrame imported from a txt file with the following structure:
ID Place Name Other
0 123456789 1100 NAME1 5468.85
1 NUMBER1 1100 DESCRIPTION1
2 STR1 DESCRIPTION2
3 NUMBER2 OTHER_STR
4 987654321 1100 NAME2 4566.65
1 NUMBER1 1100 DESCRIPTION1
2 STR1 DESCRIPTION2
I want to check something like the code below, but I understand that iterating through a df is a bad practice, and I'm not an expert in Pandas:
for row in df:
if row['Other'] == '' or row['Place'] == '':
row['ID'] == previous_row['ID']
The output should look like this:
ID Place Name Other
0 123456789 1100 NAME1 5468.85
1 123456789 1100 DESCRIPTION1
2 123456789 DESCRIPTION2
3 123456789 OTHER_STR
4 987654321 1100 NAME2 4566.65
1 987654321 1100 DESCRIPTION1
2 987654321 DESCRIPTION2
Note that any row can be either a STR, an INT or blank. The data set is a bit more than a million rows by 15 columns, so it needs to be fast.
I've tried what's suggested here , but it doesn't quite determines a condition for the value of a column to be updated.
Using pandas.Series.ffill
:
s = df["Place"].eq("") | df["Other"].eq("")
df.loc[s, "ID"] = pd.np.nan
df["ID"].ffill(inplace=True)
print(df)
Output:
ID Place Name Other
0 123456789 1100 NAME1 5468.85
1 123456789 1100 DESCRIPTION1
2 123456789 DESCRIPTION2
3 123456789 OTHER_STR
4 987654321 1100 NAME2 4566.65
1 987654321 1100 DESCRIPTION1
2 987654321 DESCRIPTION2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.