Python 将.txt文件(所有数据都作为列名的空数据框)转换为数据框

Python 将.txt文件(所有数据都作为列名的空数据框)转换为数据框,python,dataframe,txt,Python,Dataframe,Txt,我在显示.txt文件的内容时得到以下输出: Empty DataFrame Columns: [[{'city': 'Zurich, Switzerland', 'cost': '135.74'}, {'city': 'Basel, Switzerland', 'cost': '135.36'}, {'city': 'Lausanne, Switzerland', 'cost': '131.24'}, {'city': 'Lug

我在显示.txt文件的内容时得到以下输出:

        Empty DataFrame
        Columns: [[{'city': 'Zurich, Switzerland', 'cost': '135.74'}, {'city': 'Basel, 
        Switzerland', 'cost': '135.36'}, {'city': 'Lausanne, Switzerland', 'cost': '131.24'}, 
        {'city': 'Lugano, Switzerland', 'cost': '130.32'}, {'city': 'Geneva, Switzerland', 
        'cost': '130.14'}, {'city': 'Bern, Switzerland', 'cost': '125.86'}, {'city': 'Tromso, 
        Norway', 'cost': '114.81'}, {'city': 'Stavanger, Norway', 'cost': '108.38'} etc.]

有人知道如何将其转换为包含“城市”和“成本”列的数据框吗?Pandas.DataFrame()不起作用,它输出与原始文件相同的字典列表。

如果您已经有一个具有相同键的dict列表,您应该能够执行以下操作:

pandas.__version__ ->>  '1.1.5'

dctlst = [{"a": 1, "b":1}, {"a":2, "b":2}]
from pandas import DataFrame
df = DataFrame(dctlst)
df
   a  b
0  1  1
1  2  2
否则,您可以使用json生成一个字典列表

但首先,你必须清理一下文本(阅读后):

如果不移除额外的左括号和其他内容,json将不会加载它。 此外,json需要双引号,因此将单引号替换为双引号:

txt = txt.replace("'", '"')
txt
'[{"city": "Zurich, Switzerland", "cost": "135.74"}, {"city": "Basel,         
Switzerland", "cost": "135.36"}, {"city": "Lausanne, Switzerland", "cost": "131.24"},         
{"city": "Lugano, Switzerland", "cost": "130.32"}, {"city": "Geneva, Switzerland",         
"cost": "130.14"}, {"city": "Bern, Switzerland", "cost": "125.86"}, {"city": "Tromso,         
Norway", "cost": "114.81"}, {"city": "Stavanger, Norway", "cost": "108.38"} ]'
现在它看起来像一个合适的字典列表,可以通过json.loads进行转换

from json import loads
from pandas import DataFrame

lst = loads(txt)
df = DataFrame(lst)

df
                         city    cost
0         Zurich, Switzerland  135.74
1  Basel,         Switzerland  135.36
2       Lausanne, Switzerland  131.24
3         Lugano, Switzerland  130.32
4         Geneva, Switzerland  130.14
5           Bern, Switzerland  125.86
6      Tromso,         Norway  114.81
7           Stavanger, Norway  108.38
如果希望包含城市的行看起来更漂亮,可以查看字符串操作:

这会起作用,但显然取决于您想要什么:

df["city"] = df["city"].astype("string").str.replace(" ","")

    df
               city    cost
0    Zurich,Switzerland  135.74
1     Basel,Switzerland  135.36
2  Lausanne,Switzerland  131.24
3    Lugano,Switzerland  130.32
4    Geneva,Switzerland  130.14
5      Bern,Switzerland  125.86
6         Tromso,Norway  114.81
7      Stavanger,Norway  108.38
这将使它变得更好:

df[["city", "country"]] = df["city"].str.split(",", expand= True)

df
        city    cost      country
0     Zurich  135.74  Switzerland
1      Basel  135.36  Switzerland
2   Lausanne  131.24  Switzerland
3     Lugano  130.32  Switzerland
4     Geneva  130.14  Switzerland
5       Bern  125.86  Switzerland
6     Tromso  114.81       Norway
7  Stavanger  108.38       Norway

非常感谢您的回复!!!问题是,我不能在列表上使用replace函数,如果我对它进行迭代,错误表明我也不能在字典上使用它:“AttributeError:‘list’对象没有属性‘replace’”。您是否在自己身上添加了“etc.”作为问题发布?您能否更新您的问题,以显示您如何尝试将数据添加到数据框中?因为如果您已经有了一个具有相等键的字典列表,那么您就不需要进行任何替换和json操作。。您可以将字典列表作为唯一的参数传递给DataFrame(字典列表)。是的,我自己添加了“etc”。我编写了一个脚本,从一个网站上搜集一些数据,并将输出保存在一个名为“living_costs.txt”的文件中。如果我将此文件转换为csv文件并将其放入数据框中,则所有数据都位于列名中。使用pd.DataFrame(living_cost)函数返回与txt文件本身完全相同的输出。这就是为什么我尝试使用您建议的方法进行清理,但我无法进行清理,因为输入数据是列表/目录。假设您使用pandas.read_csv(“living_cost.csv”),则txt到csv的转换有问题,我不熟悉。嗯,这似乎有点太复杂了,我强烈建议您学习json文件。您可以简单地将刮取的数据存储在字典中,将其作为json转储,随时将其加载回dicts并将其放入DFs中。查看“”以获得一个很棒的教程,我向您保证它比将txt转换为csv要好100倍。如果你真的需要csv,在制作数据帧后使用df.to_csv。我将观看教程,非常感谢你的帮助!!!请提供您的代码的详细信息。
df[["city", "country"]] = df["city"].str.split(",", expand= True)

df
        city    cost      country
0     Zurich  135.74  Switzerland
1      Basel  135.36  Switzerland
2   Lausanne  131.24  Switzerland
3     Lugano  130.32  Switzerland
4     Geneva  130.14  Switzerland
5       Bern  125.86  Switzerland
6     Tromso  114.81       Norway
7  Stavanger  108.38       Norway
url = "https://www.numbeo.com/cost-of-living/region_rankings_current.jsp?region=150"
response = requests.get(url)

soup = BeautifulSoup(response.content, "html.parser")
table = BeautifulSoup(str(soup.find_all("table", id="t2")[0]), "html.parser")
table_body = BeautifulSoup(str(table.find_all("tbody")[0]), "html.parser")

findings = table_body.find_all('tr')

living_costs= []

for finding in findings:
    city = finding.find("a", class_="discreet_link").string
    cost = finding.find("td", style ="text-align: right").string
    living_costs.append({"city": city, "cost": cost})

for dicti in living_costs:
    for word in dicti:
        word.replace("Columns: [", "").replace("\n", "")

df = pd.DataFrame(living_costs)
print(df)