Konwertuj pandy Series do DataFrame
Mam Pandy Seria sf:
email
[email protected] [1.0, 0.0, 0.0]
[email protected] [2.0, 0.0, 0.0]
[email protected] [1.0, 0.0, 0.0]
[email protected] [4.0, 0.0, 0.0]
[email protected] [1.0, 0.0, 3.0]
[email protected] [1.0, 5.0, 0.0]
I chciałbym przekształcić go w następujący DataFrame:
index | email | list
_____________________________________________
0 | [email protected] | [1.0, 0.0, 0.0]
1 | [email protected] | [2.0, 0.0, 0.0]
2 | [email protected] | [1.0, 0.0, 0.0]
3 | [email protected] | [4.0, 0.0, 0.0]
4 | [email protected] | [1.0, 0.0, 3.0]
5 | [email protected] | [1.0, 5.0, 0.0]
Znalazłem sposób, aby to zrobić, ale wątpię, że jest to bardziej efektywne:
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)
7 answers
Zamiast tworzyć 2 tymczasowe dfs możesz po prostu przekazać je jako params w dict używając konstruktora DataFrame:
pd.DataFrame({'email':sf.index, 'list':sf.values})
Istnieje wiele sposobów na zbudowanie df, Zobacz docs
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-29 11:20:04
To_frame():
Począwszy od następującej serii, df:
email
[email protected] A
[email protected] B
[email protected] C
dtype: int64
Używam to_frame do konwersji serii na DataFrame:
df = df.to_frame().reset_index()
email 0
0 [email protected] A
1 [email protected] B
2 [email protected] C
3 [email protected] D
Teraz wystarczy zmienić nazwę kolumny i nazwę kolumny indeksu:
df = df.rename(columns= {0: 'list'})
df.index.name = 'index'
Twoja ramka danych jest gotowa do dalszej analizy.
Update: właśnie natknąłem się na ten link gdzie odpowiedzi są zaskakująco podobne do moich tutaj.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:47:26
Series.reset_index
z argumentem name
Często pojawia się przypadek użycia, w którym seria musi być promowana do ramki danych. Ale jeśli seria nie ma nazwy, to reset_index
spowoduje coś w rodzaju,
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
Gdzie widzisz nazwę kolumny to "0". Możemy to naprawić podając parametr 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
Jeśli chcesz utworzyć ramkę danych bez promowania indeks do kolumny, użyj Series.to_frame
, jak sugerowano w ta odpowiedź . Ten również obsługuje parametr name.
s.to_frame(name='B')
B
A
a 1
b 2
c 3
pd.DataFrame
Konstruktor
Możesz również zrobić to samo co Series.to_frame
, podając columns
param:
pd.DataFrame(s, columns=['B'])
B
A
a 1
b 2
c 3
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-04-23 21:02:00
Odpowiedź w jednej linii to
myseries.to_frame(name='my_column_name')
Lub
myseries.reset_index(drop=True, inplace=True) # As needed
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-08-12 20:31:43
Series.to_frame
może być użyty do konwersji Series
na DataFrame
.
# The provided name (columnName) will substitute the series name
df = series.to_frame('columnName')
Na przykład,
s = pd.Series(["a", "b", "c"], name="vals")
df = s.to_frame('newCol')
print(df)
newCol
0 a
1 b
2 c
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-04-01 20:12:17
Prawdopodobnie klasyfikowane jako nie pythoniczny sposób, aby to zrobić, ale to da wynik, który chcesz w linii:
new_df = pd.DataFrame(zip(email,list))
Wynik:
email list
0 [email protected] [1.0, 0.0, 0.0]
1 [email protected] [2.0, 0.0, 0.0]
2 [email protected] [1.0, 0.0, 0.0]
3 [email protected] [4.0, 0.0, 3.0]
4 [email protected] [1.0, 5.0, 0.0]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-07-30 14:24:12
Super prosty sposób jest również
df = pd.DataFrame(series)
Zwróci DF 1 kolumny ( wartości serii) + 1 indeks (0....n)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2021-01-13 16:19:49