cree un algorithme avec pandas qui permet de prendre chaque premier mot d’un fichier excel in python

To create an algorithm using Pandas that can retrieve the first word of each cell in a .xlsx file, you can use the following steps:

  1. Import the pandas library
  2. Read the .xlsx file using the pandas.read_excel() function
  3. Create a new column and apply the str.split() function to split each cell into a list of words
  4. Retrieve the first element of each list of words using the str[0] syntax
  5. Save the modified dataframe in a new .xlsx file using pandas.ExcelWriter().
main.py
# import pandas library
import pandas as pd

# read the xlsx file
df = pd.read_excel('your_file.xlsx')

# create a new column with the first word of each cell 
df['First Word'] = df['Column Name'].str.split().str[0]

# save the modified dataframe to a new xlsx file
with pd.ExcelWriter('modified_file.xlsx') as writer:
   df.to_excel(writer, index=False)
355 chars
13 lines

In the above code, replace the Column Name with the name of the column that contains the text you want to split into words. The First Word column will contain the first word of each cell. Finally, change your_file.xlsx with the name of your Excel file.

gistlibby LogSnag