팬더 시리즈를 데이터 프레임으로 변환
나는 판다 시리즈 sf:
email
email1@email.com [1.0, 0.0, 0.0]
email2@email.com [2.0, 0.0, 0.0]
email3@email.com [1.0, 0.0, 0.0]
email4@email.com [4.0, 0.0, 0.0]
email5@email.com [1.0, 0.0, 3.0]
email6@email.com [1.0, 5.0, 0.0]
그리고 이를 다음과 같은 데이터 프레임으로 변환하고자 합니다.
index | email | list
_____________________________________________
0 | email1@email.com | [1.0, 0.0, 0.0]
1 | email2@email.com | [2.0, 0.0, 0.0]
2 | email3@email.com | [1.0, 0.0, 0.0]
3 | email4@email.com | [4.0, 0.0, 0.0]
4 | email5@email.com | [1.0, 0.0, 3.0]
5 | email6@email.com | [1.0, 5.0, 0.0]
저는 그것을 할 방법을 찾았지만, 그것이 더 효율적인지 의심스럽습니다.
df1 = pd.DataFrame(data=sf.index, columns=['email'])
df2 = pd.DataFrame(data=sf.values, columns=['list'])
df = pd.merge(df1, df2, left_index=True, right_index=True)
2개의 임시 dfs를 만드는 대신 DataFrame 생성자를 사용하여 다음과 같은 매개 변수를 딕트 내에서 전달할 수 있습니다.
pd.DataFrame({'email':sf.index, 'list':sf.values})
df를 구성하는 방법은 여러 가지가 있습니다. 문서를 참조하십시오.
to_frame():
다음 시리즈로 시작, df:
email
email1@email.com A
email2@email.com B
email3@email.com C
dtype: int64
to_frame을 사용하여 시리즈를 DataFrame으로 변환합니다.
df = df.to_frame().reset_index()
email 0
0 email1@email.com A
1 email2@email.com B
2 email3@email.com C
3 email4@email.com D
이제 열 이름을 바꾸고 인덱스 열 이름을 지정하기만 하면 됩니다.
df = df.rename(columns= {0: 'list'})
df.index.name = 'index'
데이터 프레임을 추가 분석할 준비가 되었습니다.
업데이트: 저는 방금 이 링크를 발견했는데, 여기 있는 저의 답과 놀랍도록 유사합니다.
한 줄로 대답하면 다음과 같습니다.
myseries.to_frame(name='my_column_name')
또는
myseries.reset_index(drop=True, inplace=True) # As needed
Series.reset_index
와 함께name
논쟁
종종 시리즈를 데이터 프레임으로 승격해야 하는 사용 사례가 발생합니다.하지만 시리즈에 이름이 없다면,reset_index
다음과 같은 결과를 초래할 것입니다.
s = pd.Series([1, 2, 3], index=['a', 'b', 'c']).rename_axis('A')
s
A
a 1
b 2
c 3
dtype: int64
s.reset_index()
A 0
0 a 1
1 b 2
2 c 3
여기서 열 이름은 "0"입니다.우리는 이것을 고칠 수 있습니다. 그것을 지정하는 것입니다.name
매개 변수
s.reset_index(name='B')
A B
0 a 1
1 b 2
2 c 3
s.reset_index(name='list')
A list
0 a 1
1 b 2
2 c 3
Series.to_frame
인덱스를 열로 승격하지 않고 데이터 프레임을 생성하려면Series.to_frame
본 회답에서 제시한 바와 같이이름 매개 변수도 지원합니다.
s.to_frame(name='B')
B
A
a 1
b 2
c 3
pd.DataFrame
생성자
또한 다음과 같은 작업을 수행할 수 있습니다.Series.to_frame
a를 지정하여columns
매개변수:
pd.DataFrame(s, columns=['B'])
B
A
a 1
b 2
c 3
초단순한 방법은 또한
df = pd.DataFrame(series)
1열(계열 값) + 1개의 인덱스(0....n)의 DF를 반환합니다.
Series.to_frame
변환하는 데 사용할 수 있습니다.Series
로.DataFrame
.
# The provided name (columnName) will substitute the series name
df = series.to_frame('columnName')
예를들면,
s = pd.Series(["a", "b", "c"], name="vals")
df = s.to_frame('newCol')
print(df)
newCol
0 a
1 b
2 c
아마도 이를 위한 비전통적인 방법으로 등급을 매겼지만, 이것은 당신이 원하는 결과를 줄 것입니다.
new_df = pd.DataFrame(zip(email,list))
결과:
email list
0 email1@email.com [1.0, 0.0, 0.0]
1 email2@email.com [2.0, 0.0, 0.0]
2 email3@email.com [1.0, 0.0, 0.0]
3 email4@email.com [4.0, 0.0, 3.0]
4 email5@email.com [1.0, 5.0, 0.0]
언급URL : https://stackoverflow.com/questions/26097916/convert-pandas-series-to-dataframe
'programing' 카테고리의 다른 글
팬더를 사용하여 python에서 Excel 파일 읽기 (0) | 2023.06.25 |
---|---|
MS SQL Server의 형식 번호(백분율) (0) | 2023.06.25 |
여러 컨트롤러를 사용하는 Spring Boot API? (0) | 2023.06.25 |
깃허브에 있는 요지에 대해 풀 요청을 할 수 있습니까? (0) | 2023.06.25 |
gitstash pop이 stash 항목에서 추적되지 않은 파일을 복원할 수 없다고 하는 이유는 무엇입니까? (0) | 2023.06.25 |