Thank you so much for your help! It is great to have access to such a great community :) edited
I'm desperately trying to solve this for two days now, so I really hope to get some help from you all.
I have a table that looks like this:
| ColumnA | ColumnB | ColumnC |
|---|---|---|
| This | 1 | |
| Das | 2 | |
| is | 3 | |
| ist | 4 | |
| a | 5 | |
| ein | 6 | |
| not_translated | 7 | |
| text | Text | 8 |
| , | 9 | |
| 10 | ||
| , | 11 | |
| and | und | 12 |
| so | 13 | |
| so | 14 | |
| . | 15 | |
| . | 16 |
I now want that every translation in ColumnB is shifted one line up when there is nothing written in Column A (so that "This" and "Das" is in the same row), but stays in the same row when there is text in ColumnA (as in "text" and "Text"). So the result I want to achieve is:
| ColumnA | ColumnB | ColumnC |
|---|---|---|
| This | Das | 1 |
| is | ist | 3 |
| a | ein | 5 |
| not_translated | 7 | |
| text | Text | 8 |
| , | , | 9 |
| and | und | 12 |
| so | so | 13 |
| . | . | 15 |
What I've tried so far is to use a boolean, which hasn't worked out:
df1['bool'] = (df1['ColumnA'].isnull())
if df1['bool']:
df1['ColumnB'].shift(periods=-1)
else:
df1['ColumnB']
and also by using the np.where:
df1["ColumnB"] = np.where((df1["ColumnA"].isnull()), df1["ColumnB"].shift(periods=-1), df1["ColumnB"])
It would be perfect to shift up the translation in ColumnB to the next row of ColumnA with text (this was my original try, but unfortunately it didn't work out). Thank you very much for your help and efforts so far!