我想获得一个看起来像这样的列表列表(让我们称之为'All'):
[['Albany County, Wyoming', '56', '001'],
['Big Horn County, Wyoming', '56', '003'],
['Campbell County, Wyoming', '56', '005'],...
并将其转换为如下所示的数据框:
COUNTY STATE FIPS1 FIPS2
'Albany County' 'Wyoming' '56' '001'
'Big Horn County' 'Wyoming' '56' '003'
'Campbell County' 'Wyoming' '56' '005'
这是我到目前为止列出的清单:
for index, line in enumerate(All):
All[index] = line[0].split(','), line[1:]
pd.DataFrame(All)
但这又回来了:
0 1
0 [Albany County, Wyoming] [56, 001]
1 [Big Horn County, Wyoming] [56, 003]
2 [Campbell County, Wyoming] [56, 005]
提前感谢您的帮助!
答案 0 :(得分:2)
All = [
['Albany County, Wyoming', '56', '001'],
['Big Horn County, Wyoming', '56', '003'],
['Campbell County, Wyoming', '56', '005']
]
pd.DataFrame(
[line[0].split(', ') + line[1:] for line in All],
columns=['COUNTY', 'STATE', 'FIPS1', 'FIPS2']
)
COUNTY STATE FIPS1 FIPS2
0 Albany County Wyoming 56 001
1 Big Horn County Wyoming 56 003
2 Campbell County Wyoming 56 005
或者
df = pd.DataFrame(All, columns=['COUNTY, STATE', 'FIPS1', 'FIPS2'])
col = 'COUNTY, STATE'
df[col].str.extract(
'(?P<COUNTY>.*), (?P<STATE>.*)', expand=True
).join(df.drop(col, 1))
COUNTY STATE FIPS1 FIPS2
0 Albany County Wyoming 56 001
1 Big Horn County Wyoming 56 003
2 Campbell County Wyoming 56 005
答案 1 :(得分:0)
你也可以试试这个:
lst = [['Albany County, Wyoming', '56', '001'],
['Big Horn County, Wyoming', '56', '003'],
['Campbell County, Wyoming', '56', '005']]
df = pd.DataFrame(lst)
df.columns = ['COUNTY_STATE', 'FIPS1', 'FIPS2']
print(df)
COUNTY_STATE FIPS1 FIPS2
0 Albany County, Wyoming 56 001
1 Big Horn County, Wyoming 56 003
2 Campbell County, Wyoming 56 005
ALL = pd.DataFrame(df['COUNTY_STATE'].str.split(',').tolist(), columns = ['COUNTY','STATE'])
ALL[['FIPS1', 'FIPS2']] = df[['FIPS1', 'FIPS2']]
print(ALL)
COUNTY STATE FIPS1 FIPS2
0 Albany County Wyoming 56 001
1 Big Horn County Wyoming 56 003
2 Campbell County Wyoming 56 005