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.
I will use , BTW do not name your list as 'list'
df.loc[df.NAME.isin(['name1', 'name4']),'V']=1
df
NAME V ID
0 name1 1 424
1 name2 0 123
2 name3 1 241
First change variable name list to L because builtins and then replace by numpy.where with Series.isin for check membership:
L = ['name1', 'name4']
df['V'] = np.where(df.NAME.isin(L), 1, df.NAME)
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 []
You 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