数据:
one | two | |
---|---|---|
0 | a;b | 4.2 |
1 | b;c | 0.03 |
2 | a | 0 |
使用merge_column()
函数即可:
import pandas as pd
def merge_column(dataframe, column, seq=';') -> set:
merge_sentence = set()
all_sentences = dataframe[column].values.tolist()
for s in all_sentences:
if not pd.isna(s):
for i in s.split(seq):
merge_sentence.add(i)
return merge_sentence
if __name__ == '__main__':
df: pd.DataFrame = pd.DataFrame([
['a;b', 4.2],
['b;c', 0.03],
['a', 0]
], columns=['one', 'two'])
print(merge_column(df, column='one', seq=';'))