import pandas as pd
# read in the file
df = pd.read_csv('test.csv', header=None)
# .isupper to check a character
# .join to join the letters back into a string
# [:4] to slice the first 4
df['upper'] = df[0].apply(lambda x: ''.join([letter for letter in x if letter.isupper()])[:4])
print(df)
0 upper
1AAOIAppli AAOI
2ABEOAbeon ABEO
3ABIOARCA ABIO
4ABUSArbut ABUS
5ACERAcer ACER
# save only upper to a new file
df['upper'].to_csv('new_file.csv', index=False)
如果你想要所有的大写字母而不是前4个
df['upper'] = df[0].apply(lambda x: ''.join([letter for letter in x if letter.isupper()]))
0 upper
1AAOIAppli AAOIA
2ABEOAbeon ABEOA
3ABIOARCA ABIOARCA
4ABUSArbut ABUSA
5ACERAcer ACERA
使用
pandas
如果你想要所有的大写字母而不是前4个
相关问题 更多 >
编程相关推荐