Reputation: 1539
I have a dataframe that has a single row with a problem. The problem is that one of the values (from column 1) is missing, and all of the values from the remaining columns (there are 12 total columns) are shifted one to the left. Column 12 has a value of NaN.
Example: (I will simply use integers to represent my issue)
Row should be: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
I have: 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, NaN
The value in column 1 is 'missing'. The values columns 2-12 are all shifted one to the left, and column 12 is NaN.
I want to shift the values in just this one row one column to the right, so that I get: 1, NaN, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
Then I can simply replace the NaN in column 2 with the correct value.
I found shift(): https://www.geeksforgeeks.org/python-pandas-dataframe-shift/ (plus other sources)
I tried this:
playstore = playstore.iloc[10472].shift(1, axis=1)
playstore.iloc[14072:1] = 'Lifestyle'
print(playstore.tail(), '\n')
But I get a syntax error:
ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'>
Dropping axis=1 does not fix the problem. Even if it did, I am shifting the whole row from the beginning.
Is there a way to shift a single row (or rows) starting from a specific column?
Upvotes: 0
Views: 780
Reputation: 992
If you know the row you want to change and also the column number you can instead of shifting the values overwrite the row.
So imagine this:
A B C
0 1 j 1.0
1 2 k NaN
2 3 l 3.0
We know the wrong value is the column B
and is the row number 1
so we change this row:
NUM_ROW = 1 # The number of the row with has to be fixed
TOTAl_ROWS = df.shape[1] - 1 # Number of rows without counting the last one which is NaN.
new_line = [df.iloc[NUM_ROW, 0], None] # Initialize the new_line with the right part of the row.
# Get the rest of the new row.
for i in df.iloc[NUM_ROW, 1:TOTAl_ROWS]:
new_line.append(i)
# Overwriting old row with new one
df.loc[NUM_ROW] = new_line
So your output is:
A B C
0 1 j 1
1 2 None k
2 3 l 3
Upvotes: 1