목록을 Pandas 데이터 프레임 열로 변환
제 목록을 한 칸짜리 판다 데이터 프레임으로 변환해야 합니다.
현재 목록(len=3):
['Thanks You',
'Its fine no problem',
'Are you sure']
필수 Pandas DF(모양 = 3,:
0 Thank You
1 Its fine no problem
2 Are you sure
N.B. 숫자는 위의 필수 Pandas DF에 있는 색인을 참조하십시오.
사용:
L = ['Thanks You', 'Its fine no problem', 'Are you sure']
#create new df
df = pd.DataFrame({'col':L})
print (df)
col
0 Thanks You
1 Its fine no problem
2 Are you sure
df = pd.DataFrame({'oldcol':[1,2,3]})
#add column to existing df
df['col'] = L
print (df)
oldcol col
0 1 Thanks You
1 2 Its fine no problem
2 3 Are you sure
감사합니다 DYZ:
#default column name 0
df = pd.DataFrame(L)
print (df)
0
0 Thanks You
1 Its fine no problem
2 Are you sure
목록이 [1,2,3]과 같으면 다음을 수행할 수 있습니다.
import pandas as pd
lst = [1,2,3]
df = pd.DataFrame([lst])
df.columns =['col1','col2','col3']
df
가져오기:
col1 col2 col3
0 1 2 3
또는 다음과 같이 열을 생성할 수 있습니다.
import numpy as np
import pandas as pd
df = pd.DataFrame(np.array([lst]).T)
df.columns =['col1']
df
가져오기:
col1
0 1
1 2
2 3
메소드를 직접 호출하여 목록을 매개 변수로 전달할 수 있습니다.
import pandas as pd
l = ['Thanks You', 'Its fine no problem', 'Are you sure']
pd.DataFrame(l)
출력:
0
0 Thanks You
1 Its fine no problem
2 Are you sure
목록이 여러 개 있는데 이 목록으로 데이터 프레임을 만들고 싶다면,다음과 같이 수행할 수 있습니다.
import pandas as pd
names = ["A", "B", "C", "D"]
salary = [50000, 90000, 41000, 62000]
age = [24, 24, 23, 25]
data = pd.DataFrame([names, salary, age]) # Each list would be added as a row
data = data.transpose() # To Transpose and make each rows as columns
data.columns = ['Names', 'Salary', 'Age'] # Rename the columns
data.head()
출력:
Names Salary Age
0 A 50000 24
1 B 90000 24
2 C 41000 23
3 D 62000 25
예:
['Thank You',
'It\'s fine no problem',
'Are you sure?']
코드 블록:
import pandas as pd
df = pd.DataFrame(lst)
출력:
0
0 Thank You
1 It's fine no problem
2 Are you sure?
Pandas 데이터 프레임의 열 이름은 제거하지 않는 것이 좋습니다.그러나 여전히 헤더가 없는 데이터 프레임을 원할 경우(질문에 게시한 형식에 따름) 다음 작업을 수행할 수 있습니다.
df = pd.DataFrame(lst)
df.columns = ['']
출력은 다음과 같습니다.
0 Thank You
1 It's fine no problem
2 Are you sure?
또는
df = pd.DataFrame(lst).to_string(header=False)
그러나 출력은 데이터 프레임 대신 목록이 됩니다.
0 Thank You
1 It's fine no problem
2 Are you sure?
목록을 Pandas 코어 데이터 프레임으로 변환하려면 Pandas 패키지의 DataFrame 방법을 사용해야 합니다.
위의 작업을 수행하는 방법은 여러 가지가 있습니다(판다가 다음과 같이 수입된다고 가정).pd
)
pandas.DataFrame({'Column_Name':Column_Data})
- 열_이름 : 문자열
- 열_데이터 : 목록 양식
-
Data = pandas.DataFrame(Column_Data)` Data.columns = ['Column_Name']
그래서, 위에서 언급한 문제에 대해, 코드 스니펫은
import pandas as pd
Content = ['Thanks You',
'Its fine no problem',
'Are you sure']
Data = pd.DataFrame({'Text': Content})
list = ['Thanks You', 'Its fine no problem', 'Are you sure']
df = pd.DataFrame(list)
출력:
0
0 Thanks You
1 Its fine no problem
2 Are you sure
열 이름:
df.columns = ['col name']
또한assign()
기존 데이터 프레임에 대한 목록입니다.이 기능은 여러 메서드를 체인으로 묶고 나중에 체인에서 사용해야 하는 열을 할당해야 할 경우 특히 유용합니다.
df = pd.DataFrame()
df1 = df.assign(col=['Thanks You', 'Its fine no problem', 'Are you sure'])
언급URL : https://stackoverflow.com/questions/42049147/convert-list-to-pandas-dataframe-column
'programing' 카테고리의 다른 글
.NET에서 정수로 목록을 쉽게 채울 수 있는 방법 (0) | 2023.06.02 |
---|---|
루비의 "not"과 "!"의 차이 (0) | 2023.06.02 |
ubuntu에서 postgresql을 완전히 제거하고 다시 설치하는 방법은 무엇입니까? (0) | 2023.06.02 |
오류: psycopg2라는 모듈이 없습니다.내선 번호 (0) | 2023.06.02 |
ExpressJS 애플리케이션을 구성하는 방법은 무엇입니까? (0) | 2023.06.02 |