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.
Replacing certain values from entire columns of a pandas dataframe
python - pandas replace values of a list column - Stack Overflow
python - Replace list element in pandas dataframe - Stack Overflow
python - how to replace an entire column on Pandas.DataFrame - Stack Overflow
Hi, I created a pandas dataframe with one column called 'service' with 100+ rows. some of the values are 1, 2, and 3. i want to replace each with a word. so for example, whenever the value is 2, it instead prints as "Fun Pro". under the same column. thanks for any help
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 []
This is not a trivial problem, because DataFrames are not designed to work with mutable objects like lists, sets, or dicts.
You can determine the index of match and replace accordingly.
m = [v == [4, 5] for v in df['a']]
df.loc[m, 'a'] = 4.5
df
a
0 1
1 2
2 3
3 4.5
4 [apple, pear]
A similar procedure follows for ['apple', 'pair']. You can form a function from this if you so wish:
def replace(df, col, key, val):
m = [v == key for v in df[col]]
df.loc[m, col] = val
replace(df, 'a', [4, 5], 4.5)
replace(df, 'a', ['apple', 'pear'], 'apple')
df
a
0 1
1 2
2 3
3 4.5
4 apple
Note: The function works in-place.
There is one way using astype , Even it work , but I still highly recommend you using cold's answer.
df.astype(str).replace({'[4, 5]':4.5,"['apple', 'pear']":"apple"})
Out[159]:
a
0 1
1 2
2 3
3 4.5
4 apple
If the indices match then:
df['B'] = df1['E']
should work otherwise:
df['B'] = df1['E'].values
will work so long as the length of the elements matches
If you don't mind getting a new data frame object returned as opposed to updating the original Pandas .assign() will avoid SettingWithCopyWarning. Your example:
df = df.assign(B=df1['E'])