我想将一列拆分为多列,并使用Python为每个值分配相同的列名。
a sample of code is here
import pandas as pd
df = pd.DataFrame({"grade": ["a,b,c", "d,e,f", "b,d,a", "a,b,c,d,e,f"]})
I have used split function
# split column into multiple columns by delimiter
df[['Grade_A', 'Grade_B', 'Grade_C', 'Grade_D', 'Grade_E', 'Grade_F']] =
df['grade'].str.split(',', expand=True)
and got different values in columns name for example in column Grade_a, I got *a, d, b, a*, instead I wan to get *a, a, NA*.
What I really want to find out is the output of this code:
df = pd.DataFrame({"grade": ["a,b,c,d,e,f", "d,e,f", "b,d,a", "a,b,c,d,e,f"],
"Grade_A": ["a", "NA", "a", "a"],
"Grade_B": ["b", "NA", "b", "b"],
"Grade_c": ["c","NA","NA", "c"],
"Grade_D": ["d","d", "d", "d"],
"Grade_E": ["e","e","NA", "e"],
"Grade_F": ["f","f", "NA", "f"],
})
I have solved this problem in excel and R program, but I really want in python. Does any can help me?
1条答案
按热度按时间bnl4lu3b1#
可能的解决方案:
输出: