Je suis novice en Python. J'ai ce code Python pour séparer les colonnes d'un tableau csv :
import pandas as pd
import re
df = pd.read_csv("test.csv")
# obtenir les données de traffic
name_regex = r"(?<=(\'name\': ))[^,]*(?=,)"
medium_regex = r"(?<=(\'medium\': ))[^,]*(?=,)"
source_regex = r"(?<=(\'source\': ))[^,]*(?=})"
def get_name(x):
return [re.search(name_regex, line).group().replace("'", "") for line in x.split("\n")]
def get_medium(x):
return [re.search(medium_regex, line).group().replace("'", "") for line in x.split("\n")]
def get_source(x):
return [re.search(source_regex, line).group().replace("'", "") for line in x.split("\n")]
df["traffic_name"] = df["traffic"].apply(get_name)
df["traffic_medium"] = df["traffic"].apply(get_medium)
df["traffic_source"] = df["traffic"].apply(get_source)
del df["traffic"]
final_df = df.explode(["traffic_name", "traffic_medium", "traffic_source"])
print(final_df)
final_df.to_csv("traffic.csv")
voici mes données csv :
,traffic_source
0,"{'name': '(test1)', 'medium': '(no1)', 'source': '(yes)'}"
1,
2,"{'name': '(test1)', 'medium': 'no2', 'source': 'yes1'}"
3,
4,"{'name': '(test2)', 'medium': 'no3', 'source': 'yes'}"
lorsque j'exécute le code python, il y a une erreur. Pouvez-vous s'il vous plaît aider ? Merci
~\AppData\Local\Temp\ipykernel_15228\2852501840.py in get_name(x)
12
13 def get_name(x):
---> 14 return [re.search(name_regex, line).group().replace("'", "") for line in x.split("\n")]
15 def get_medium(x):
16 return [re.search(medium_regex, line).group().replace("'", "") for line in x.split("\n")]
AttributeError: 'float' object has no attribute 'split'