Python 要将列表转换为字符串,请参见下面的示例
我从一列Python 要将列表转换为字符串,请参见下面的示例,python,python-3.x,Python,Python 3.x,我从一列df['Test']中得到一个结果集。head(): 我希望将其转换为以下格式,并存储在与以下格式相同的列中: fed official say weak data caused weather slow taper fed 's Charles plosser see high bar change pace tapering 使用.apply(“.join) Ex: import pandas as pd df = pd.DataFrame({'Test': [['fed',
df['Test']中得到一个结果集。head()
:
我希望将其转换为以下格式,并存储在与以下格式相同的列中:
fed official say weak data caused weather slow taper
fed 's Charles plosser see high bar change pace tapering
使用.apply(“.join)
Ex:
import pandas as pd
df = pd.DataFrame({'Test': [['fed', 'official', 'say', 'weak', 'data', 'caused', 'weather', 'slow', 'taper'],
['fed', "'s", 'charles', 'plosser', 'see', 'high', 'bar', 'change', 'pace', 'tapering']
]
})
print(df["Test"].apply(" ".join))
0 fed official say weak data caused weather slow...
1 fed 's charles plosser see high bar change pac...
Name: Test, dtype: object
>>> import pandas as pd
>>> df = pd.DataFrame({'Test': ["[fed, official, say, weak, data, caused, weather, slow, taper]",
"[fed, 's, charles, plosser, see, high, bar, change, pace, tapering]"
]
})
>>> df['Test']=df['Test'].str[1:-1].str.split(', ').apply(' '.join)
>>> df
Test
0 fed official say weak data caused weather slow...
1 fed 's charles plosser see high bar change pac...
输出:
import pandas as pd
df = pd.DataFrame({'Test': [['fed', 'official', 'say', 'weak', 'data', 'caused', 'weather', 'slow', 'taper'],
['fed', "'s", 'charles', 'plosser', 'see', 'high', 'bar', 'change', 'pace', 'tapering']
]
})
print(df["Test"].apply(" ".join))
0 fed official say weak data caused weather slow...
1 fed 's charles plosser see high bar change pac...
Name: Test, dtype: object
>>> import pandas as pd
>>> df = pd.DataFrame({'Test': ["[fed, official, say, weak, data, caused, weather, slow, taper]",
"[fed, 's, charles, plosser, see, high, bar, change, pace, tapering]"
]
})
>>> df['Test']=df['Test'].str[1:-1].str.split(', ').apply(' '.join)
>>> df
Test
0 fed official say weak data caused weather slow...
1 fed 's charles plosser see high bar change pac...
由于您的列表格式不正确 首先我转换成单词列表,然后转换成字符串 代码:
import pandas as pd
df = pd.DataFrame({'Test': [['fed', 'official', 'say', 'weak', 'data', 'caused', 'weather', 'slow', 'taper'],
['fed', "'s", 'charles', 'plosser', 'see', 'high', 'bar', 'change', 'pace', 'tapering']
]
})
print(df["Test"].apply(" ".join))
0 fed official say weak data caused weather slow...
1 fed 's charles plosser see high bar change pac...
Name: Test, dtype: object
>>> import pandas as pd
>>> df = pd.DataFrame({'Test': ["[fed, official, say, weak, data, caused, weather, slow, taper]",
"[fed, 's, charles, plosser, see, high, bar, change, pace, tapering]"
]
})
>>> df['Test']=df['Test'].str[1:-1].str.split(', ').apply(' '.join)
>>> df
Test
0 fed official say weak data caused weather slow...
1 fed 's charles plosser see high bar change pac...
输出:
import pandas as pd
df = pd.DataFrame({'Test': [['fed', 'official', 'say', 'weak', 'data', 'caused', 'weather', 'slow', 'taper'],
['fed', "'s", 'charles', 'plosser', 'see', 'high', 'bar', 'change', 'pace', 'tapering']
]
})
print(df["Test"].apply(" ".join))
0 fed official say weak data caused weather slow...
1 fed 's charles plosser see high bar change pac...
Name: Test, dtype: object
>>> import pandas as pd
>>> df = pd.DataFrame({'Test': ["[fed, official, say, weak, data, caused, weather, slow, taper]",
"[fed, 's, charles, plosser, see, high, bar, change, pace, tapering]"
]
})
>>> df['Test']=df['Test'].str[1:-1].str.split(', ').apply(' '.join)
>>> df
Test
0 fed official say weak data caused weather slow...
1 fed 's charles plosser see high bar change pac...
试试这个:
for index, row in df.iterrows() :
s = ''
for i in row['Test'] :
s = s + i
df['Test'][index] = s
谢谢你们的帮助 df['Test'].apply(lambda x::.join([Word(Word).lemmatize()表示x.split()中的单词))
它在上述场景中起作用。您是否尝试在数据帧的每一行上使用
'.join()
?尝试了,但出现了错误','。join(s)错误-名称“fed”未定义您已经尝试了什么?我们是来帮忙的,不是为你做的;)列表中的数据应为数据类型字符串,即用引号括起来,如“data”
。所以,把你的清单列成这样-[“联邦储备银行”,“官方”,…]
。你能告诉我们你已经尝试了什么吗。我尝试使用df['Test'].apply(lambda x:“”.join([Word(Word).lemmatize()表示x.split()中的单词))