This should do it for you:
# Find the name of the column by index
n = df.columns[1]
# Drop that column
df.drop(n, axis = 1, inplace = True)
# Put whatever series you want in its place
df[n] = newCol
...where [1] can be whatever the index is, axis = 1 should not change.
This answers your question very literally where you asked to drop a column and then add one back in. But the reality is that there is no need to drop the column if you just replace it with newCol.
This should do it for you:
# Find the name of the column by index
n = df.columns[1]
# Drop that column
df.drop(n, axis = 1, inplace = True)
# Put whatever series you want in its place
df[n] = newCol
...where [1] can be whatever the index is, axis = 1 should not change.
This answers your question very literally where you asked to drop a column and then add one back in. But the reality is that there is no need to drop the column if you just replace it with newCol.
newcol = [..,..,.....]
df['colname'] = newcol
This will keep the colname intact while replacing its contents with newcol.
python - pandas replace values of a list column - Stack Overflow
pandas - Replacing few values in a column based on a list in python - Stack Overflow
python - Replace items in column based on list - Stack Overflow
Best way to replace values in one column from another column in pandas?
You were pretty close to the solution.
What I did was:
data.replace({'Good': '1', 'Average': '2', 'Bad': '3'}, regex=True)
and obtain the result that you were looking:
enter image description here
For me second solution working, but necessary convert strings to lists before:
import ast
df['Feedback'] = df['Feedback'].apply(ast.literal_eval)
#df['Feedback'] = df['Feedback'].str.strip('[]').str.split(',')
First solution working with nested dictionary:
df = df.assign(Feedback=[[feedback_dict.get(i,i) for i in x] for x in df['Feedback']])
df['Feedback'] = df['Feedback'].apply(lambda x : [feedback_dict.get(i,i) for i in list(x)])
print (df)
ID Feedback
0 T223 [1, 3, 3]
1 T334 [2, 1, 1]
EDIT: If instead lists are missing values use if-else statement - non list values are replaced to empty lists:
print (df)
ID Feedback
0 T223 [Good,Bad,Bad]
1 T334 [Average,Good,Good]
2 NaN NaN
feedback_dict = {'Good':1, 'Average':2, 'Bad':3}
df = df.assign(Feedback=[[feedback_dict.get(i,i) for i in x] if isinstance(x, list) else []
for x in df['Feedback']])
print (df)
ID Feedback
0 T223 [1, 3, 3]
1 T334 [2, 1, 1]
2 NaN []
Original range:
| old_items | new_items |
|---|---|
| item1 | item6 |
| item2 | 0 |
| item3 | item7 |
| item4 | 0 |
| item5 | item8 |
Desired output:
| old_items | new_items |
|---|---|
| item6 | item6 |
| item2 | 0 |
| item7 | item7 |
| item4 | 0 |
| item8 | item8 |
My stupid solution:
old_items = list(df['old_items'])
new_items = list(df['new_items'])
proper_items = []
for x in range(len(old_items)):
if new_items[x] != 0:
proper_items.append(new_items[x])
else:
proper_items.append(old_items[x])
df['old_items'] = proper_itemsYou can set the Sample column as index and then replace values on the whole data frame based on conditions:
df = df.set_index('Sample')
df[df < -1] = np.nan
df[(df < 0) & (df > -1)] = 0.05
Which gives:
# element1 element2 element3
# Sample
# alpha 1.00 1.0 NaN
# beta 0.05 1.0 1.00
# gamma NaN 1.0 1.00
# delta 1.00 NaN 0.05
# epsilon NaN 2.0 2.00
Here is the successful answer as suggested by @Psidom.
The solution involves taking a slice out of the dataframe, applying the function, then reincorporates the amended slice:
df1 = df.loc[:, headings]
df1[df1 < -1] = np.nan
df1[(df1 < 0)] = 0.05
df.loc[:, headings] = df1